简体   繁体   中英

Can't configure nginx location(use rewrite)?

I have a server running nginx, which is accessible to the Internet through port 80. Let's say the external IP address of my server is 8.41.37.217, and I have a domain srv.vvzvlad.xyz, which points to this address.So when I open h[tt]p://srv.vvzvlad.xyz in my browser, I see the nginx welcome page. So far everything is working.

I have docker running on the server with some containers that implement web applications. These applications inside the docker containers open ports 80, and outside the containers (from the internal network) these ports are available as 3333 or 4444 or similar. So, by opening the local server address (192.168.88.111) in the browser with port 3333, I access the port of the docker container where the wiki is running: h[tt]p://192.168.88.111:3333 redirects to 10.0.0.2:80, where 10.0.0.2 is the internal address of the docker container. This also works.

Now I want to access these docker containers from the internet. I could just open ports on the router, and go to addresses like h[tt]p://srv.vvzvlad.xyz:3333, but some software that will send requests doesn't know how to handle ports other than 80/443. That's why I set up some locations on nginx (see the first paragraph) which redirect URLs like h[tt]p://srv.vvzvlad.xyz(:80)/wiki to h[tt]p://192.168.88.111:3333.

I do it like this:

server {
    listen 80;
...
    location /wiki {
        proxy_pass h[tt]p://192.168.88.111:3333;
    }
...
}

My network structure, which I just described, looks something like this: https://habrastorage.org/webt/ud/va/8p/udva8p6ggl_ojb3cde-yessdnmk.png

This scheme works, but is added to any address inside the application "/wiki". Thus, the address of the application is h[tt]p://192.168.88.111:3333/index in h[tt]p://srv.vvzvlad.xyz/wiki/index. With some applications this works fine. But some applications expect to run exclusively at the root of the web server. They work fine with the address h[tt]p://srv.vvzvlad.xyz directly (h[tt]p://srv.vvzvlad.xyz/index), but cannot work with the added address (h[tt]p://srv.vvzvlad.xyz/wiki/index), because they still use internally absolute links to their resources like h[tt]p://srv.vvzvlad.xyz/favicon.png, which of course are not on "location /", but only on "location /wiki".

How do I solve this problem? I've tried using rewrite(eg "rewrite ^/wiki/(.*)$ h[tt]p://192.168.88.111:3000/$1;"), but the resources the application requests still remain without /wiki: https://habrastorage.org/webt/zj/s4/oa/zjs4oazs7ywo10c_zffxs8pkpnw.png

Use a trailing slash for both should help for most. Otherwise play around with the slashes.. it is always a pain..

 location /wiki/ {
        proxy_pass http://192.168.88.111:3333/;

/wiki is regexing for all terms wiki*

Also a possibility:

 location ~* /wiki/(.*)$ {
        proxy_pass http://192.168.88.111:3333/$1;

If you can't reach /wiki (without trailing slash) in browser after that:

 location /wiki {
        return 308 http://$host/wiki/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM