Nginx的安装:https://zhangguoli.cn/linux-nginx-install.html

假设有3台服务器,分别是:
A : 10.10.1.1 Nginx
B : 10.10.6.2:8090 bbb
C : 10.10.6.3:8086 ccc
没有配置Nginx代理前,B访问地址是 10.10.6.2:8090/bbb ,C的访问地址是10.10.6.3:8086/ccc
配置代理之后, B、C的访问地址分别是10.10.1.1/bbb ,10.10.1.1/ccc

修改nginx.conf

文件位置:./nginx/conf/nginx.conf

1. 在http和server节点分别include配置文件

http {
    include       mime.types;
    default_type  application/octet-stream;

    include bbb_pool;
    include ccc_pool;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

        include bbb_proxy;
        include ccc_proxy;

下面创建bbb_pool,ccc_pool,bbb_proxy,ccc_proxy文件,其相对路径如下:

./nginx/conf/nginx.conf
./nginx/conf/bbb_pool
./nginx/conf/ccc_pool
./nginx/conf/bbb_proxy
./nginx/conf/ccc_proxy

2. bbb_pool和ccc_pool包含服务器IP,Port配置信息

upstream bbb_pool{
        server 10.10.6.2:8090;
}
upstream ccc_pool{
        server 10.10.6.3:8086;
}

3.bbb_proxy和ccc_proxy包含地址对应关系

location之后配置的是Nginx的路径
proxy_pass配置的是应用原访问路径

location ^~ /bbb/ {
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header REMOTE-HOST $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header Host $host;
                 proxy_pass http://bbb_pool/bbb/;
                 proxy_redirect off;
}
location ^~ /ccc/ {
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header REMOTE-HOST $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header Host $host;
                 proxy_pass http://ccc_pool/ccc/;
                 proxy_redirect off;
}

4、重新加载配置文件

./nginx/sbin/nginx -s reload

5、访问10.10.1.1/bbb ,10.10.1.1/ccc可以访问应用

标签: nginx

添加新评论