nginx 设置多个代理服务器(nginx多代理)

修改配置文件 nginx.conf

修改前的内容,如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        # 处理跨域问题,nginx用的是80端口,tomcat用的是8080端口,这就存在跨域的情况
        # 将请求路径中的有/api/的,将其前面和其内容替换为http://localhost:8080/
        # 比如:http://localhost/api/account/login
        # 替换:http://localhost:8080/account/login
        location /api/ {
		    proxy_pass http://localhost:8080/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

在 server 配置项中,增加 一个 location 的配置内容,如下:

        # 设置多代理
        # 将请求路径中的有/apu/的,将其前面和其内容替换为http://localhost:8081/
        # 比如:http://localhost/apu/account/info
        # 替换:http://localhost:8081/account/info
        location /apu/ {
            proxy_pass http://localhost:8081/;
        }

修改后的内容,如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        # 处理跨域问题,nginx用的是80端口,tomcat用的是8080端口,这就存在跨域的情况
        # 将请求路径中的有/api/的,将其前面和其内容替换为http://localhost:8080/
        # 比如:http://localhost/api/account/login
        # 替换:http://localhost:8080/account/login
        location /api/ {
		    proxy_pass http://localhost:8080/;
        }

        # 设置多代理
        # 将请求路径中的有/apu/的,将其前面和其内容替换为http://localhost:8081/
        # 比如:http://localhost/apu/account/info
        # 替换:http://localhost:8081/account/info
        location /apu/ {
            proxy_pass http://localhost:8081/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

运行 cmd ,进入到 nginx 的根目录(可以先打开 nginx 的根目录,在地址栏中输入cmd按回车)

启动 nginx 服务,start nginx

重启 nginx 服务,nginx -s reload

停止 nginx 服务,nginx -s stop

猜你喜欢

转载自blog.csdn.net/BillKu/article/details/143426466