nginx反向代理配置好比但愿经过浏览器输入http://localhost 直接跳转到http://www.baidu.com 进入到百度首页php
在nginx/conf.d/default.conf中配置:html
server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://www.baidu.com;//请求转向自定义的服务器列表 跳转到百度 proxy_redirect default; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
下面对nginx.conf文件进行说明:node
nginx进程数,建议设置为等于CPU总核心数nginx
单个进程最大链接数,那么该服务器的最大链接数=链接数*进程数web
监听端口通常都为http端口:80;正则表达式
域名能够有多个,用空格隔开:例如 server_name www.baidu.com;浏览器
负载均衡列表基本配置:服务器
•location / {}:对aspx后缀的进行负载均衡请求,假如咱们要对全部的aspx后缀的文件进行负载均衡时,能够这样写:location ~ .*\.aspx$ {}app
•proxy_pass:请求转向自定义的服务器列表,这里咱们将请求都转向标识为http://cuitccol.com的负载均衡服务器列表;负载均衡
在负载均衡服务器列表的配置中,weight是权重,能够根据机器配置定义权重(若是某台服务器的硬件配置十分好,能够处理更多的请求,那么能够 为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么能够将前一台的weight配置为weight=2,后一台差的配置为 weight=1)。weigth参数表示权值,权值越高被分配到的概率越大;
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. upstream web_pools{ server 192.168.102.101:80 weight=1; server 192.168.102.102:22222 weight=3; } include /etc/nginx/conf.d/*.conf; }
/etc/nginx/conf.d/default.d 配置以下:
# # The default server # #upstream baidu{ # server https://www.baidu.com/; #} server { listen 80 default_server; listen [::]:80 default_server; server_name baidu weixin; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://web_pools; proxy_redirect default; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
配置参数可参考(https://www.zybuluo.com/phper/note/89391)
常见配置
user root; worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { upstream yytest { server 192.168.102.101:80; } upstream zztest { server 192.168.102.102:80; } upstream xxtest { server 192.168.102.103:20003; } include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 80 ; server_name yytest.witpos.cn; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://yytest; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 80 ; server_name zztest.witpos.cn; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://wsctest; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 80 ; server_name zztest.witpos.cn; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://zztest; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,由于全部请求都是以"/"开始 # 可是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并中止匹配 其它location [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 可是全部 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }
请求URI例子:
参考文章:
http://www.jianshu.com/p/bed000e1830b
http://www.jianshu.com/p/4b9e00408837