Nginx反向代理和负载均衡部署方法

发布时间:2021-08-16 10:25:46 作者:chen
来源:亿速云 阅读:148

本篇内容介绍了“Nginx反向代理和负载均衡部署方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

nginx反向代理与负载均衡

  1. upstream proxy.test.com {


  2.              #ip_hash;


  3.              server 192.168.6.119:9090;


  4.              server 192.168.6.119:8090;



  5.          }




  6. server {

  7.         listen 80;

  8.         server_name proxy.test.com;

  9.         location / {

  10.           proxy_set_header Host $host;


  11.           proxy_set_header X-Real-IP $remote_addr;


  12.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


  13.  


  14.              #禁用缓存


  15.              proxy_buffering off;


  16.  


  17.              #反向代理的地址


  18.              proxy_pass http://proxy.test.com;

  19.         }


  20.     }

[root@node3 vhost]# curl proxy.test.com
119
[root@node3 vhost]# curl proxy.test.com
nginx2
[root@node3 vhost]# curl proxy.test.com
119
[root@node3 vhost]# curl proxy.test.com
nginx2


线上某机器举例:反向代理和负载均衡

  1. upstream oc_haodai_com {

  2.         server 192.168.1.37:8080 weight=2 max_fails=2 fail_timeout=30s;

  3.       # server 192.168.1.38:8080 weight=2 max_fails=2 fail_timeout=30s;

  4.       #server 192.168.1.39:8080 weight=2 max_fails=2 fail_timeout=30s;

  5. }


  6. server {

  7.         listen 443;    ###https的默认端口  http是80

  8.     

  9.         server_name oc.haodai.com;

  10.         ssl on;

  11.          ssl_certificate /usr/local/nginx1.8/conf/haodai.com.crt;

  12.          ssl_certificate_key /usr/local/nginx1.8/conf/haodai.com.key;

  13.         location / {

  14.                 proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

  15.                 proxy_set_header Host $host;

  16.                 proxy_set_header X-Real-IP $remote_addr;

  17.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  18.                 #session_sticky_hide_cookie upstream=account.free4lab.com;

  19.                 proxy_pass http://oc_haodai_com;

  20.         }


  21.     access_log /home/wwwlogs/oc.haodai.com.proxy.log access;

  22. }





  23. server {

  24.         listen 80;


  25.         server_name oc.haodai.com;

  26.         location / {

  27.                 proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

  28.                 proxy_set_header Host $host;

  29.                 proxy_set_header X-Real-IP $remote_addr;

  30.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  31.                 #session_sticky_hide_cookie upstream=account.free4lab.com;

  32.                 proxy_pass http://oc_haodai_com;

  33.         }


  34. }


37.38.39上配置文件

  1. [root@order_web04 vhost]# more oc.haodai.com.conf

  2. server {

  3.         listen       8080;

  4.         server_name  oc.haodai.com;

  5.         index index.html index.htm index.php;

  6.         root  /home/wwwroot/order_center;

  7.         access_log /home/wwwlogs/oc.haodai.com.log access;

  8.         location / {

  9.             index index.php;

  10.             if ( !-e $request_filename){

  11.                 rewrite ^/(.*)$ /index.php?s=$1 last;

  12.                 break;

  13.             }

  14.         }



  15.         location ^~ /.git {

  16.             return 403;

  17.         }



  18.         location ~ /Common/sh/.*\.(php|php5|sh|py|pl)$ {

  19.             deny all;

  20.         }


  21.         if ($request_uri ~* "/Common/") {

  22.                 return 403;

  23.         }


  24.         if ($request_uri ~* "/Common/sh") {

  25.                 return 403;

  26.         }


  27.         location ~ .*\.(php|php5)?$ {

  28.             try_files $uri =404;

  29.             fastcgi_pass  127.0.0.1:9000;

  30.             fastcgi_index index.php;

  31.             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

  32.             include fastcgi_params;

  33.         }


  34.         if ( $fastcgi_script_name ~ \..*\/.*php ) {

  35.             return 403;

  36.         }

  37.   }

“Nginx反向代理和负载均衡部署方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Nginx反向代理概况及负载均衡详解
  2. 浅谈Nginx七层反向代理和负载均衡

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx

上一篇:怎么用Python下载抖音无水印视频

下一篇:windows上怎么安装mysql

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》