简体   繁体   中英

Nginx Reverse proxy config

I'm having issues with getting a simple config to work with nginx. I have a server that host docker containers so nginx is in a container. So lets call the url foo.com . I would like for the url foo.com/service1 to actually just go to foo.com on another port, so it would actually be pulling foo.com:4321 and foo.com/service2 to be pulling foo.com:5432 and so on. Here is the config I have been having issues with.

http {
    server {
        listen 0.0.0.0:80;

        location /service1/ {
            proxy_pass http://192.168.0.2:4321/;
        }

        location /service2/ {
            proxy_pass http://192.168.0.2:5432/;
        }

    }
}

So the services and nginx live at 192.168.0.2. What is the prefered way to be able to do this? Thank you in advance!

As A side note, this is running in a docker container. Thanks!

I think you should check whether your foo.com is pointing to the right ip address or not first by removing the reverse proxy config. Eg

http {
    server {
        listen 80;
        server_name foo.com;

        location / {
        }
    }
}

Then, if your ip address already has a service running on port 80 you should specify the server_name for each service like in my example. Nginx can only distinguish which service to respond to which domain by server_name .

*My guess is that you forgot the server_name option.

http {
    server {
        listen 80;
        server_name foo.com;

        location /service1/ {
            proxy_pass http://192.168.0.2:4321/;
        }

        location /service2/ {
            proxy_pass http://192.168.0.2:5432/;
        }

    }
}

I have a guess your problem is not related to Nginx per se, but instead it is related to Docker networking. You provided insufficient information to make a detailed conclusion, but here it is a couple of suggestions:

  1. run a simple Docker container at the same host where nginx container is running and try curl from inside that container (I've seen your answer that you are able to call curl from the server running Nginx, but it's not really the same)

  2. for example, if the server running nginx container is OSX or Windows, it may use an intermediate Linux virtual machine with its own network stack, IP addreses, routing, etc.

This is my conf sending to inner glassfish. Check out the use of proxy_redirect off & proxy_set_header X-NginX-Proxy true;

    #Glassfish
    location /MyService/   {

            index index.html;

            add_header Access-Control-Allow-Origin $http_origin;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-NginX-Proxy true;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:18000/MyService/;

    }

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