DWQA QuestionsCategory: Development ToolHow does Nginx reverse proxy multiple ports to different directories on the same port?
jmyz_0455 asked 1 month ago

How to configure the Nginx reverse proxy so that multiple ports can be proxied to different directories on the same port?
I have applications a, B and C running on ports 8001, 8002 and 8003 respectively.
The server does not configure the domain name, so the access address isip:8001ip:8002andip:8003
Now I want to implement it with Nginx, open only one port of 80, and then access different services through different directories.
I expect access addresses to becomeip:80/aip:80/bandip:80/c。 And all resource requests under this application are based on this path.
such asip:80/aCan jump toip:80/a/loginRather thanip:80/loginNotice the difference in the catalogue.
My/etc/nginx/conf.d/default.conf is as follows:

server {
    listen       80;
    server_name  localhost;
    location /a {
        proxy_pass http://127.0.0.1:8001/;
    }
    location /b {
        proxy_pass http://127.0.0.1:8002/;
    }
    location /c {
        proxy_pass http://127.0.0.1:8003/;
    }
}

According to the configuration above, enterip:80/aI did see a successful agency.ip:8001Application, just look at the home page, everything is normal.
But a jumpip:80/a/user/**Such a multilevel directory fails and becomesip:80/user/**
I’ve also tried to use official documents, regular configuration locations or rewrite to achieve the desired results.
Thank you for your advice.

6 Answers
Best Answer
jmyz_0455 answered 1 month ago

I tried. No, unless there is a domain name.

Yujiaao answered 1 month ago

Mapping directories, the back end of the agent should be changed accordingly, otherwise the relationship between directories can not be maintained one by one. Rewrite is also a URL rewriting, which cannot rewrite the URL of a tag of HTML on the back-end page. A great deal of trouble is worth nothing.
A more reasonable way is to use secondary domain name to correspond to different back-end services.

server {
        listen       80;
        server_name  sub1.example.com;
        location / {
                proxy_pass http://127.0.0.1:8001;
        }       
    }
    server {
        listen 80;
        server_name  sub2.example.com;
        location / {
               proxy_pass http://127.0.0.1:8002;
        }
    }
    ......
Harman answered 1 month ago

Here’s the problem:proxy_pass http://127.0.0.1:8001/;
This means absolute path, so he will ignore your / A.
You change to:proxy_pass http://127.0.0.1:8001;
That’s all right. If the answer is right, take it.

tim1020 answered 1 month ago

Change to regular matching after location

tim1020 replied 1 month ago

Sorry, I didn’t see your problem clearly.As I understand it, after you put ip:80/a proxy to ip:8001, the corresponding path should also be ip:8001/a, which means that when you deploy 8001, you should put the project in a directory. In that case, visit ip:80/a/user./Just visit ip: 8001/a/user/equally

LoyaltyLu answered 1 month ago

You’d better watch it.Official website