Nginx设置反向代理

在没有root权限下设置反向代理和负载均衡。由于只有一个IP地址,只设置Nginx服务器,因此,只能通过设置不同的端口号实现反向代理功能。将8080端口赋予代理服务器(要从浏览器访问该端口需要预先设置Linux的防火墙,要不然无法访问),web1和web2为两个后端服务器,端口分别为7070和6060。第一步需要在html文件下建立web1和web2的访问显示页面,分别对应为test1.html(访问页面显示为:“hello,test1..com”)和test2.html(hello,“test2.com”)。

(1)设置nginx.conf的相关项

events {

 13     worker_connections  1024;
 14 }
 15
 16
 17 http {

 18    ....

 33
 34 upstream webserver {
 35         server 127.0.0.1:7070 weight=1;    #127.0.0.1   localhost的IP地址.web1的地址和端口
 36         server 127.0.0.1:6060 weight=1;    #web2的地址和端口 
 37 }
 38

 39     #gzip  on;
 40
 41     server {
 42        # listen       80;
 43         listen       8080;
 44         server_name  localhost;
 47
 48
 49
 50           #charset koi8-r;
 51
 52           #access_log  logs/host.access.log  main;
 53
 54           location / {
 55             #  root   html;
 56              # index  index.html index.htm;
 57                 proxy_pass http://webserver;    #将跳转到后端服务器

 60            }
 61
 62         #error_page  404              /404.html;
 63
 64         # redirect server error pages to the static page /50x.html
 65         #
 66         error_page   500 502 503 504  /50x.html;
 67         location = /50x.html {
 68             root   html;
 69         }
 70

 71        }

 server {                                                                 #设置web1服务器。web2与此类似。将7070改为6060,test2改为test1
 78        # listen       80;
 79         listen       7070;
 80         server_name  localhost;

 81

      location / {
120             root   html;
121             index  test2.html test2.htm;
122         }

(2)reload nginx

再从浏览器访问8080端口时显示的是hello,test1.com,刷新一下显示hello,test2.com,依次循环。因为在默认情况下负载调度是按顺序的。

 86         location / {
 87             root   html;
 88             index  test1.html test1.htm;
 89 

你可能感兴趣的:(Nginx)