1. 核心配置文件主要由三个部分构成
    • 基本配置
    • events 配置
    • http配置
    1. 配置文件
    1. // 在conf 文件夹下的 nginx.conf 文件
    2. #################################基本配置
    3. #user nobody; # 配置worker 进程运行用户
    4. worker_processes 1; # 配置工作进程数目,根据硬件调整,通常等于cpu数量或者2倍于cpu数量
    5. #error_log logs/error.log; # 配置全局错误日志及类型 [debug, info, notice, warn, error, crit] 默认是error
    6. #error_log logs/error.log notice;
    7. #error_log logs/error.log info;
    8. #pid logs/nginx.pid; # 进程编号
    9. #################################events配置
    10. #配置工作模式和连接数
    11. events {
    12. worker_connections 1024; # 配置每个worker进程连接数上限,nginx支持的总连接数就等于worker_process * worker_connections
    13. }
    14. #################################http配置
    15. http {
    16. #-------------http基本配置---------------------
    17. include mime.types; # 配置nginx 支持哪些多媒体类型, 可以再conf/mime.types 查看支持哪些多媒体类型
    18. default_type application/octet-stream; # 默认文件类型
    19. # 配置日志格式
    20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    21. # '$status $body_bytes_sent "$http_referer" '
    22. # '"$http_user_agent" "$http_x_forwarded_for"';
    23. #配置access.log 日志及其存放路径,并使用的上面定义的main日志格式
    24. #access_log logs/access.log main; # 这里的main 就是上面配置的日志的格式
    25. sendfile on; # 开启高效文件传输模式
    26. #tcp_nopush on; # 防止网络阻塞
    27. #keepalive_timeout 0;
    28. keepalive_timeout 65; # 长连接超时时间, 单位是秒
    29. #gzip on; # 开启gzip压缩输出 on表示开启
    30. # 轮询策略 默认
    31. upstream www.xiangdeyizhang.xyz {
    32. server 192.168.1.137:9555;
    33. server 192.168.1.137:9554;
    34. server 192.168.1.137:9553;
    35. }
    36. # 配置负载均衡 权重策略
    37. upstream www.xiangdeyizhang.xyz {
    38. server 192.168.1.137:9555 weight=3;
    39. server 192.168.1.137:9554 weight=1;
    40. server 192.168.1.137:9553 weight=2;
    41. }
    42. # ip_hash ip绑定策略 ip固定访问某个服务器
    43. upstream www.xiangdeyizhang.xyz {
    44. ip_hash;
    45. server 192.168.1.137:9555;
    46. server 192.168.1.137:9554;
    47. server 192.168.1.137:9553;
    48. }
    49. # least_conn 最少连接策略,每次连接,就会连接到,连接次数最少的那台
    50. upstream www.xiangdeyizhang.xyz {
    51. least_conn
    52. server 192.168.1.137:9555;
    53. server 192.168.1.137:9554;
    54. server 192.168.1.137:9553;
    55. }
    56. #-------------http server区域配置----------------
    57. #### 配置虚拟主机 可以看到下面还有别的server 对象, 也就是说 这里可以配置很多的server 大概会有200 多个左右吧 每个的配置其实都是差不多的
    58. server {
    59. listen 80; # 配置监听端口号 范围为 0 - 65535
    60. server_name localhost; # 配置服务名 localhost 表示任何ip都是可以进行访问的
    61. # 配置负载均衡的代理地址 代理地址
    62. # proxy_paxx 后面跟的地址要和 upstream 后面跟的名称要是一样的
    63. # 例如: upstream abcde 那么 proxy_pass http://abcde
    64. location / {
    65. proxy_pass http://www.xiangdeyizhang.xyz;
    66. }
    67. # 代理地址例如:
    68. location /api {
    69. proxy_pass http://dev.do.xmfunny.com/api/v1;
    70. # expires 作用于http, server, location, 可以控制HTTP应答中的 'Expires' 'Cache-Control'的头标 起到控制页面缓存的作用
    71. #如果expires为-1s,即永远过期,则reponse headerCache-Control: no-cache expires为正值,则headerCache-Control: max-age = #
    72. expires 1d;
    73. }
    74. # 配置静态代理 js|html|css|zip|gz|png|jpg|jpeg|doc|docx|pdf|ppt 为后缀的文件 都去这个地址进行访问
    75. location ~ .*\.(js|html|css|zip|gz|png|jpg|jpeg|doc|docx|pdf|ppt)$ {
    76. root E:\static
    77. }
    78. #charset koi8-r; #配置字符集 koi8-r 是俄罗斯字符集 我们中国可以改为utf-8字符集
    79. #access_log logs/host.access.log main; # 与上面基本配置里面的日志是一样的,如果上面下面同时配置了 就以下面的为主 配置本虚拟主机的访问日志
    80. # location 是用于匹配路由的,这里的意思就是匹配 / 的路由, 因为你访问localhost:8080 的时候就会默认访问带 / 的路由
    81. # localhost:8080 就是 localhost:8080/ 的缩写显示
    82. # 这里就是匹配到 / 路由 然后做一个界面的展示 跟前端路的意思差不多
    83. location / {
    84. # 这里的root 就相当于 ip + port localhost:8080 = root = ip + port
    85. root E:\ace; # root 是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录, 实际中我们要定位到项目的html目录中 例如打包好的dist目录中
    86. index index.html index.htm; # 配置首页文件的名称 可省略 会自动去寻找文件夹下的index.html
    87. }
    88. #error_page 404 /404.html; # 配置404界面,当访问不到这个界面的时候弹出一个404界面
    89. # redirect server error pages to the static page /50x.html
    90. #
    91. error_page 500 502 503 504 /50x.html; # 同理 配置50x界面,只要是符合这个匹配规则的都会进入到这个界面
    92. # 有等号 表示精确匹配
    93. location = /50x.html {
    94. root html;
    95. }
    96. # PHP 脚本请求全部转发到Apache处理
    97. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    98. #
    99. #location ~ \.php$ {
    100. # proxy_pass http://127.0.0.1;
    101. #}
    102. # PHP 脚本请求全部转发到FastCGI处理
    103. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    104. #
    105. #location ~ \.php$ {
    106. # root html;
    107. # fastcgi_pass 127.0.0.1:9000;
    108. # fastcgi_index index.php;
    109. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    110. # include fastcgi_params;
    111. #}
    112. # 禁止访问 .htaccess文件 这个文件中,会有很多的路径,这个文件中的路径表示外网不能访问这些文件
    113. # deny access to .htaccess files, if Apache's document root
    114. # concurs with nginx's one
    115. #
    116. #location ~ /\.ht {
    117. # deny all;
    118. #}
    119. }
    120. # 配置另外一个虚拟主机
    121. # another virtual host using mix of IP-, name-, and port-based configuration
    122. #
    123. #server {
    124. # listen 8000;
    125. # listen somename:8080;
    126. # server_name somename alias another.alias;
    127. # location / {
    128. # root html;
    129. # index index.html index.htm;
    130. # }
    131. #}
    132. # 举例: 配置https 服务, 安全的网络传输协议, 加密传输
    133. # HTTPS server
    134. #
    135. #server {
    136. # listen 443 ssl; # 端口默认为443
    137. # server_name localhost;
    138. # ssl_certificate cert.pem; # 证书 需要进行购买
    139. # ssl_certificate_key cert.key; # 证书 需要进行购买
    140. # ssl_session_cache shared:SSL:1m;
    141. # ssl_session_timeout 5m;
    142. # ssl_ciphers HIGH:!aNULL:!MD5;
    143. # ssl_prefer_server_ciphers on;
    144. # location / {
    145. # root html;
    146. # index index.html index.htm;
    147. # }
    148. #}
    149. }