配置文件

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. }

文件结构

nginx文件结构分为如下情况

  1. ... #全局块
  2. events { #events块
  3. ...
  4. }
  5. http #http块
  6. {
  7. ... #http全局块
  8. server #server块
  9. {
  10. ... #server全局块
  11. location [PATTERN] #location块
  12. {
  13. ...
  14. }
  15. location [PATTERN]
  16. {
  17. ...
  18. }
  19. }
  20. server
  21. {
  22. ...
  23. }
  24. ... #http全局块
  25. }

各文件配置块的作用如下:

  1. 全局块:配置影响nginx全局的指令,一般为:
  • 运行nginx的用户组
  • nginx进程pid存放路径
  • 日志存放路径
  • 配置文件引入
  • 允许生成worker process数
  1. events块:配置影响nginx服务器或与用户的网络连接
  • 每个进程的最大连接数
  • 选取哪一种事件驱动模型处理连接请求
  • 是否允许同时接受多个网路连接
  • 开启多个网路连接序列化
  1. http块:可以嵌套多个server,每一个server就是一个服务,配置代理,缓存,日志定义等绝大多数的功能和第三方模块的引入

例如:

  • 文件引入
  • mime-type定义
  • 日志自定义
  • 是否使用sendfile传输文件
  • 连接超时时间
  • 单连接请求数
  1. server块:配置虚拟主机的相关参数,算是最主要的模块
  2. location块:配置请求的路由,以及各种页面的处理请求

示例:
nginx配置文件中每一个指令必须以分号作结束标记

  1. #user administrator administrators; #配置用户或者组,默认为nobody nobody。
  2. #worker_processes 2; #允许生成的进程数,默认为1
  3. #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
  4. error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg
  5. events {
  6. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  7. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  8. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  9. worker_connections 1024; #最大连接数,默认为512
  10. }
  11. http {
  12. include mime.types; #文件扩展名与文件类型映射表
  13. default_type application/octet-stream; #默认文件类型,默认为text/plain
  14. #access_log off; #取消服务日志
  15. log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  16. access_log log/access.log myFormat; #combined为日志格式的默认值
  17. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  18. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  19. keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
  20. upstream mysvr {
  21. server 127.0.0.1:7878;
  22. server 192.168.10.121:3333 backup; #热备
  23. }
  24. error_page 404 https://www.baidu.com; #错误页
  25. server {
  26. keepalive_requests 120; #单连接请求上限次数。
  27. listen 4545; #监听端口
  28. server_name 127.0.0.1; #监听地址
  29. location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  30. #root path; #根目录
  31. #index vv.txt; #设置默认页
  32. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  33. deny 127.0.0.1; #拒绝的ip
  34. allow 172.18.5.54; #允许的ip
  35. }
  36. }
  37. }

反向代理配置

设置404导向页面

  1. error_page 404 https://www.runnob.com; #错误页
  2. proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。

配置客户端请求方式

  1. proxy_method get; #支持客户端的请求方法:post/get;

设置支持的请求协议

  1. proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本

负载均衡

超时返回

设置连接超时时间,让负载均衡的响应等待时间更少

  1. proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
  2. proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
  3. proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
  4. proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。

也可以使用upstream指令配置备用服务器,当配置的负载均衡的服务器返回配置的响应状态值时,就启用备用服务器参与负载均衡

  1. proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。

状态值如下:

  • error:建立连接或者向被代理的服务器发送请求或者读取响应信息时服务器发生错误
  • timeout:建立连接,向被代理服务器发送请求或读取响应信息时服务器发生超时
  • invalid_header:被代理服务器返回的响应头异常
  • off:无法将请求分发给被代理的服务器
  • http_400:被代理服务器返回的状态码为400,500,502等

获取真实IP

可以在nginx中进行配置获取真实IP
这样获取到的IP就不是代理服务器的IP地址了

  1. proxy_set_header Host $host; #只要用户在浏览器中访问的域名绑定了 VIP VIP 下面有RS;则就用$host ;host是访问URL中的域名和端口 www.taobao.com:80
  2. proxy_set_header X-Real-IP $remote_addr; #把源IP 【$remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP;这样在代码中 $X-Real-IP来获取 源IP
  3. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用 【,】隔开;代码中用 echo $x-forwarded-for |awk -F, '{print $1}' 来作为源IP

参考配置:

upstream

upstream这个配置是写一组被代理的服务器地址,然后配置负载均衡的算法。这里的被代理服务器地址有两种写法

  1. upstream mysvr {
  2. server 192.168.10.121:3333;
  3. server 192.168.10.122:3333;
  4. }
  5. server {
  6. ....
  7. location ~*^.+$ {
  8. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  9. }
  10. }

负载配置

热备

热部署备用服务器

  • 如果你有2台服务器,当一台服务器发生事故无法正常访问时,就会启动第二台服务器

    1. upstream mysvr {
    2. server 127.0.0.1:7878; #A
    3. server 192.168.10.121:3333 backup; #热备B
    4. }

    请求顺序为AAAA……(A宕机)BBBB

    轮询

  • 两台服务器进行轮询分发请求,默认权重为1

    1. upstream mysvr {
    2. server 127.0.0.1:7878;
    3. server 192.168.10.121:3333;
    4. }

    请求顺序为ABABABABAB

加权轮询

因为服务器有的性能好,有的性能差,根据配置的权重的大小而分发给不同的服务器不同的请求,默认权重为1

  1. upstream mysvr {
  2. server 127.0.0.1:7878 weight=1;
  3. server 192.168.10.121:3333 weight=2;
  4. }

请求顺序为ABBABBABBABBABB

IP_hash

第一次请求会随机分发服务器,但是以后的请求nginx会让相同的客户端IP请求相同的服务器

  1. upstream mysvr {
  2. server 127.0.0.1:7878;
  3. server 192.168.10.121:3333;
  4. ip_hash;
  5. }

配置参数:

  • down:标识服务器暂时不参与负载均衡
  • backup:标识服务器作为热备服务器
  • mx_fails:允许请求失败的次数,默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误
  • fail_timeout:在经历max_fail失败后,暂停服务时间,两者可以一起使用

例如:

  1. upstream mysvr {
  2. server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2;
  3. server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1;
  4. }

参考:https://www.runoob.com/w3cnote/nginx-setup-intro.html