Nginx reverse proxy - Internal servers separated by trailing slash

烂漫一生 提交于 2021-01-29 11:24:17

问题


I'm a newbie at Nginx, and have been searching a lot for the right answer to my question, but couldn't find it; not because it is not there, but my newbie condition limits me to adapt a generic solution to my issue.

The situation is this: I have a Mantis Bug Tracker in my private LAN (http://10.111.111.12). On the other hand, i have an OwnCloud website also on my LAN (IP 10.111.111.5), with URL http://10.111.111.5/owncloud/.

What i want to do is to deploy a Nginx Reverse Proxy that handles all requests from Internet at publicdomain.com, and use trailing slash for each internal webserver. The desired result would be:

http://www.publicdomain.com/bugtracker -> redirects to http://10.111.111.12/index.php http://www.publicdomain.com/cloud -> redirects to http://10.111.111.5/owncloud/ (note that "cloud" is preferred over "owncloud")

On the future, it is necessary to continue using trailing slash for other web servers to be deployed.

Questions are: is this scenario possible? if so, is it enough with configuring nginx or I have to reconfigure internal web servers as well?

I really appreciate your help, by indicating me a possible solution or pointing me to the right direction on previous posts.

Thanks a lot in advance.


回答1:


Yes it is possible to achieve such configuration and it's commonly used when NGINX is acting as a reverse proxy. You can use this configuration as an inspiration for building your own:

upstream bugtracker {
  server 10.111.111.12;
}

upstream cloudupstream {
  server 10.111.111.5;
}

server {
  listen 80;
  location /bugtracker/{
    proxy_pass http://bugtracker;
  }
  location /cloud/{
    proxy_pass http://cloudupstream/owncloud;
  }
}

What's happening over here is that nginx will be listening on port 80 and as soon as a request comes for path /bugtracker, it will automatically route the request to the upstream server mentioned above. Using this you can add as many upstream servers and location blocks as you want. Reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass




回答2:


Thanks a lot Namam for your quick answer. However, it isn't working yet. It seems that "server" at upstream directive does not allow slash, like this: server 10.111.111.5/owncloud;

If i used it, i obtained nginx: [emerg] invalid host in upstream "10.111.111.5/owncloud" in /etc/nginx/nginx.conf:43

I started with the first upstream bugtracker, only, and nginx.conf like this:

    upstream bugtracker {
        server 10.111.111.12;
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
    }

    location /mstic{
        proxy_pass http://bugtracker;
    }
        
        error_page 404 /404.html;
            location = /40x.html {
        }

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

After that, when accesing to my Nginx Reverse proxy http://10.111.111.10/mstic/ i obtain the following: Not Found The requested URL /mstic/ was not found on this server. and no further details on error or access logs.

Thanks a lot in advance for any extra help you could bring me.



来源:https://stackoverflow.com/questions/63911861/nginx-reverse-proxy-internal-servers-separated-by-trailing-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!