Question

Nginx multipe proxy pass locations

Posted June 22, 2021 148 views
Nginx

I need to proxy pass my domain to 2 different apps! the first one is the main API and the second one is a WebSocket.

My configuration looks like this but it won’t work quite well! when I hit the / it’s fine but when I go in /ws location it shows me default Nginx page 404


server {
    server_name ws.saraf.app;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ws.saraf.app/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ws.saraf.app/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location ~ / {
    proxy_pass http://127.0.0.1:8080;
        proxy_buffers 16 32k;
        proxy_buffer_size 64k;
        proxy_busy_buffers_size 128k;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_connect_timeout 59s;
        proxy_hide_header X-Powered-By;
        proxy_http_version 1.1;
        proxy_ignore_headers Cache-Control Expires;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_pass_header Set-Cookie;
        proxy_read_timeout 600;
        proxy_redirect off;
        proxy_send_timeout 600;
        proxy_temp_file_write_size 64k;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Host $host;
        proxy_set_header Proxy '';
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Original-Request $request_uri;
    }

    location ~ /ws {
    proxy_pass http://127.0.0.1:8889;
        proxy_buffers 16 32k;
        proxy_buffer_size 64k;
        proxy_busy_buffers_size 128k;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_connect_timeout 59s;
        proxy_hide_header X-Powered-By;
        proxy_http_version 1.1;
        proxy_ignore_headers Cache-Control Expires;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_pass_header Set-Cookie;
        proxy_read_timeout 600;
        proxy_redirect off;
        proxy_send_timeout 600;
        proxy_temp_file_write_size 64k;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Host $host;
        proxy_set_header Proxy '';
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Original-Request $request_uri;
    }
}


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @hasanparasteh,

The config looks good to me, all is in order. With that in mind, is there anything listening on port 8889. It seems to me this might be the culprit to further look into.

Please do see If anything is listening to that port. I’m eager to see

  • Thanks for your answer but there was no other application listening on that port. I fixed this issue by just removing some lines that I thought is useless. IDK how it is fixed. it was the weirdest problem I ever had

    • Hi @hasanparasteh,

      I’m glad to hear you managed to solve the issue.

      Weird indeed to have to remove some lines on a working config. If you happen to remember them, please do post them, I’m curious to look into it more.

      • Yeah. here is the working production configuration that I came up with:

        upstream nodes {
            hash $remote_addr consistent;
            least_conn;
        
            server localhost:8081;
            server localhost:8082;
            server localhost:8083;
            server localhost:8084;
        }
        
        server {
            listen 80 default_server;
            listen [::]:80 default_server;
        
            root /var/www/html;
            index index.html index.htm index.nginx-debian.html;
            server_name _;
            location / {
                    try_files $uri $uri/ =404;
            }
        }
        
        server {
            listen [::]:443 ssl ipv6only=on; # managed by Certbot
            listen 443 ssl; # managed by Certbot
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
            include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
            ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
        
            location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
        
                proxy_pass http://nodes;
        
                # enable WebSockets
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
            }
        
            location /publish {
                proxy_pass http://localhost:4040/publish;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
            }
        
        }