Nginx安装与配置集群负载均衡代理proxy

1.     下载Nginx

a.      登录官网http://nginx.org/下载

b.      网盘链接:https://pan.baidu.com/s/1_HC5C_PvsRfYWhFR0V89Uw密码:78a1

2.     配置

说明:配置文件在conf\nginx.conf

(1)   发布系统:

请求Url:localhost:80/index.html

物理地址:D:/Study/Study/NginxTest/index.html

    server {

        listen       80;

        server_name  127.0.0.1 localhost;

        location / {

            root   D:/Study/Study/NginxTest;

            index  index.html index.htm;

        }

   }

 

2)反向代理:

    server {

        listen       80;

        server_name  127.0.0.1 localhost;

        location / {

            root   D:/Study/Study/NginxTest;

            index  index.html index.htm;

        }

            #反向代理 : 使用正则匹配

            location ~ \.(jpg|png|jpeg|gif)$ {

            proxy_pass  http://site.com;

        }

   }

3集群负载均衡:

#网站A

server {

        listen       8099;

        server_name  localhost;

        location / {

            root   D:/Study/Study/NginxTestOne;

            index  index.html index.htm;

        }

   }

#网站B

server {

        listen       8098;

        server_name  localhost;

        location / {

            root   D:/Study/Study/NginxTestTwo;

            index  index.html index.htm;

        }

   }

  #集群/负载均衡服务器列表

   upstream outServer{

             serverlocalhost:8099 weight=1 max_fails=2 fail_timeout=3s;

             serverlocalhost:8098 weight=1 max_fails=2 fail_timeout=3s;

   }

   server {

        listen       80;

        server_name  localhost;

              keepalive_timeout  30; #连接超时3秒

        location / {

                  proxy_pass http://outServer;

        }

   }

3.     使用

说明:最好以dos命令的方式来操作

(1)切换到安装包目录:Win+R  => cmd =>cd 安装包目录

(2)nginx常用启动命令

               a.      启动:start nginx :

b.      修改配置后重新加载生效:nginx -s reload   

c.    重新打开日志文件:nginx -sreopen 
d.      测试nginx配置文件是否正确:nginx -t -c/path/to/nginx.conf

e.    快速停止nginx  nginx -s stop

f.     完整有序的停止nginxnginx -s quit 


猜你喜欢

转载自blog.csdn.net/qq_26900081/article/details/80874086
今日推荐