I recently uninstalled apache2 for Nginx I'm trying to listen on 88, 808 and 888 for my sites and redirect different subdomains for each (and another domain to another server). the problem is that Nginx is giving bad gateway for all proxied requests and timeout for the direct ip access.

proxy conf : --> otherdomain.fr = eror 502 bad gateway

# HTTP
server {
    # Listen on ipv4
    listen 80;
    #listen [::]:80;

    server_name  ~.*.otherdomain.fr;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass "http://192.168.1.17";
    }
}

server{
    listen 80;
    server_name nextcloud.domain.me;
    location / {
        proxy_set_header Host $host;
        proxy_pass "http://127.0.0.1:888";
        proxy_redirect off;
    }
}

server{
    listen 80;
    server_name domain.me;
    location / {
        proxy_set_header Host $host;
        proxy_pass "http://127.0.0.1:808";
        proxy_redirect off;
    }
}

ex for port 88: --> ip:88 = timeout

(obviously, it is enabled and the nginx user have access to the files)

server {
    # Listen on ipv4
    listen 88;
  
    location / {
        root /var/www/html/tests;
    }

}

I'm obviously doing something wrong but I cant find out what, if you could give me a hand it would be incredible. Thank you in advance!

EDIT :

netstat -tulpen | grep -E '8.?8'

tcp 0 0 127.0.0.1:10028 0.0.0.0:*
LISTEN 0 28186 1929/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 91603 8801/nginx: master tcp 0 0 127.0.0.1:12340
0.0.0.0:* LISTEN 0 25868 734/dovecot tcp 0 0 127.0.0.1:631 0.0.0.0:*
LISTEN 0 19881 497/cupsd tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 0 75033 8801/nginx: master tcp 0 0 0.0.0.0:888
0.0.0.0:* LISTEN 0 75032 8801/nginx: master tcp 0 0 0.0.0.0:443 0.0.0.0:*
LISTEN 0 75030 8801/nginx: master tcp 0
0 127.0.0.1:10025 0.0.0.0:* LISTEN 0
28182 1929/master udp6 0 0 :::32885
:::* 113 21172
482/avahi-daemon: r"""


Solution 1:

You don't need quotes around the destination URL of proxy_pass.

Main problem is the server_name ~.*.otherdomain.fr and the fact that you are using proxy_set_header Host $host.

In this case it seems your regular expression for server_name is invalid and it is taken as a string. That string appears then in $host variable.

You should try

server_name *.otherdomain.fr;

instead. If that doesn't work, use

proxy_set_header Host $http_host;

Which passes the HTTP Host header onward instead of contents of $host variable.