配置文件
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
文件结构
nginx文件结构分为如下情况
... #全局块events { #events块...}http #http块{... #http全局块server #server块{... #server全局块location [PATTERN] #location块{...}location [PATTERN]{...}}server{...}... #http全局块}
各文件配置块的作用如下:
- 全局块:配置影响nginx全局的指令,一般为:
- 运行nginx的用户组
- nginx进程pid存放路径
- 日志存放路径
- 配置文件引入
- 允许生成worker process数
- events块:配置影响nginx服务器或与用户的网络连接
- 每个进程的最大连接数
- 选取哪一种事件驱动模型处理连接请求
- 是否允许同时接受多个网路连接
- 开启多个网路连接序列化
- http块:可以嵌套多个server,每一个server就是一个服务,配置代理,缓存,日志定义等绝大多数的功能和第三方模块的引入
例如:
- 文件引入
- mime-type定义
- 日志自定义
- 是否使用sendfile传输文件
- 连接超时时间
- 单连接请求数
- server块:配置虚拟主机的相关参数,算是最主要的模块
- location块:配置请求的路由,以及各种页面的处理请求
示例:
nginx配置文件中每一个指令必须以分号作结束标记
#user administrator administrators; #配置用户或者组,默认为nobody nobody。#worker_processes 2; #允许生成的进程数,默认为1#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emergevents {accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为onmulti_accept on; #设置一个进程是否同时接受多个网络连接,默认为off#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventportworker_connections 1024; #最大连接数,默认为512}http {include mime.types; #文件扩展名与文件类型映射表default_type application/octet-stream; #默认文件类型,默认为text/plain#access_log off; #取消服务日志log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式access_log log/access.log myFormat; #combined为日志格式的默认值sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。upstream mysvr {server 127.0.0.1:7878;server 192.168.10.121:3333 backup; #热备}error_page 404 https://www.baidu.com; #错误页server {keepalive_requests 120; #单连接请求上限次数。listen 4545; #监听端口server_name 127.0.0.1; #监听地址location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。#root path; #根目录#index vv.txt; #设置默认页proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表deny 127.0.0.1; #拒绝的ipallow 172.18.5.54; #允许的ip}}}
反向代理配置
设置404导向页面
error_page 404 https://www.runnob.com; #错误页proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
配置客户端请求方式
proxy_method get; #支持客户端的请求方法:post/get;
设置支持的请求协议
proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本
负载均衡
超时返回
设置连接超时时间,让负载均衡的响应等待时间更少
proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
也可以使用upstream指令配置备用服务器,当配置的负载均衡的服务器返回配置的响应状态值时,就启用备用服务器参与负载均衡
proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。
状态值如下:
- error:建立连接或者向被代理的服务器发送请求或者读取响应信息时服务器发生错误
- timeout:建立连接,向被代理服务器发送请求或读取响应信息时服务器发生超时
- invalid_header:被代理服务器返回的响应头异常
- off:无法将请求分发给被代理的服务器
- http_400:被代理服务器返回的状态码为400,500,502等
获取真实IP
可以在nginx中进行配置获取真实IP
这样获取到的IP就不是代理服务器的IP地址了
proxy_set_header Host $host; #只要用户在浏览器中访问的域名绑定了 VIP VIP 下面有RS;则就用$host ;host是访问URL中的域名和端口 www.taobao.com:80proxy_set_header X-Real-IP $remote_addr; #把源IP 【$remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP;这样在代码中 $X-Real-IP来获取 源IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用 【,】隔开;代码中用 echo $x-forwarded-for |awk -F, '{print $1}' 来作为源IP
参考配置:
- https://www.runoob.com/w3cnote/http-x-forwarded-for.html
- https://www.runoob.com/w3cnote/nginx-setup-intro.html
upstream
upstream这个配置是写一组被代理的服务器地址,然后配置负载均衡的算法。这里的被代理服务器地址有两种写法
upstream mysvr {server 192.168.10.121:3333;server 192.168.10.122:3333;}server {....location ~*^.+$ {proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表}}
负载配置
热备
热部署备用服务器
如果你有2台服务器,当一台服务器发生事故无法正常访问时,就会启动第二台服务器
upstream mysvr {server 127.0.0.1:7878; #Aserver 192.168.10.121:3333 backup; #热备B}
轮询
两台服务器进行轮询分发请求,默认权重为1
upstream mysvr {server 127.0.0.1:7878;server 192.168.10.121:3333;}
请求顺序为ABABABABAB
加权轮询
因为服务器有的性能好,有的性能差,根据配置的权重的大小而分发给不同的服务器不同的请求,默认权重为1
upstream mysvr {server 127.0.0.1:7878 weight=1;server 192.168.10.121:3333 weight=2;}
请求顺序为ABBABBABBABBABB
IP_hash
第一次请求会随机分发服务器,但是以后的请求nginx会让相同的客户端IP请求相同的服务器
upstream mysvr {server 127.0.0.1:7878;server 192.168.10.121:3333;ip_hash;}
配置参数:
- down:标识服务器暂时不参与负载均衡
- backup:标识服务器作为热备服务器
- mx_fails:允许请求失败的次数,默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误
- fail_timeout:在经历max_fail失败后,暂停服务时间,两者可以一起使用
例如:
upstream mysvr {server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2;server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1;}
