利用nginx作为反向代理来做负载均衡是最简单的一种模式。配置灵活简单且强大,下面的文章就教大家如何在nginx1.8 上使用反向代理。
centos 两台虚拟机
1.1 192.168.0.218 作为主机
1.2 192.168.0.219 作为反向代理机
2. 192.168.0.218 例如有一个网站 有如下配置
2.1 配置
server { listen 80; server_name www.op.com; access_log /home/wwwlogs/op.access.log; error_log /home/wwwlogs/op.error.log; root /home/www/onephper/public; # 项目根目录 index index.php index.htm; error_page 404 /404.html; location = /404.html { root /usr/local/nginx/html; } location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/nginx/html; } location ~ \.php$ { root /home/www/onephper/public; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /test/ { proxy_pass http://192.168.0.219; #重点在这个地方 proxy_redirect default ; } }
2.2 也可以在http段这种配置
upstream test_op_com { server 192.168.0.219; }
然后再server段配置
location /test/ { proxy_pass http://test_op_com; proxy_redirect default; }
2.3 域名形式配置
location /test/ { proxy_pass http://www.test.com; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
3. 192.168.0.219 机器的配置
server { listen 80; server_name 192.168.0.219; access_log /home/wwwlogs/op.access.log; error_log /home/wwwlogs/op.error.log; root /home/www/onephper/public; index index.php index.htm; error_page 404 /404.html; location = /404.html { root /usr/local/nginx/html; } location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/nginx/html; } location ~ \.php$ { root /home/www/onephper/public; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
参考链接
http://liuyu.blog.51cto.com/183345/166381/
https://www.freehao123.com/nginx-google/ 域名反向
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass nginx代理官网地址
结束语
本文有任何错误,或有任何疑问,欢迎留言说明。
网友最新评论