Search code examples
nginxserverhostserver-name

nginx direct trafic to non domain name using IP and local names


Using nginx on windows server I want to direct traffic into different ports using names that are non-domain names, first one works but second one never reach: why? what is wrong? http://192.xxx.xxx.xxx/game: works http://192.xxx.xxx.xxx/cms: never reach. If I change names then cms works and game never reach.

server {
    listen       80;
    server_name  game;
    location /{
        proxy_pass http://localhost:4040;
        proxy_connect_timeout 60s;
        proxy_read_timeout 5400s;
        proxy_send_timeout 5400s;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect default;
    }
location /uploads/ {
        root c:\Ebrahimi\www\appGame;
}
}

server {
    listen       80;
    server_name  cms;
    location /{
        proxy_pass http://localhost:2010;
        proxy_connect_timeout 60s;
        proxy_read_timeout 5400s;
        proxy_send_timeout 5400s;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect default;
    }
location /uploads/ {
        root c:\Ebrahimi\www\appCms;
}
}

New code block as "Richard Smith" mentioned:

server {
    listen       80;
    location /{
        proxy_pass http://localhost:4040;
        proxy_connect_timeout 60s;
        proxy_read_timeout 5400s;
        proxy_send_timeout 5400s;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect default;
    }
location /uploads/ {
        root c:\Ebrahimi\www\appGame;
}
location /game {
        proxy_pass http://localhost:4040;
}
location /cms{
        proxy_pass http://localhost:2010;
}
}

Solution

  • The second server block can only be accessed using its server_name, for example: http://app.firouzeh-mfg.ir/

    If you are accessing your server using its IP address, the request will be processed by the first server block (or the block marked as the default_server). See this document for details.

    Your two URLs http://192.x.x.x/game and http://192.x.x.x/cms (assuming identical IP addresses) access the same server block and differ only by location.

    For example:

    server {
        location /game {
            proxy_pass http://localhost:4040;
        }
        location /cms{
            proxy_pass http://localhost:2010;
        }
    }