0

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"""

louis habert
  • 1
  • 1
  • 3
  • What's the reason to differentiate the ports and then proxying them? What's the output of `netstat -tulpen | grep -E '8.?8?'`` ? Do you have `listen` directives for all of these ports? – digijay Nov 03 '21 at 19:49
  • 1./ I'm proxying and differentiate the ports because I use a different server for each one (808 is a cherrypy serv actually not running, otherdomain.fr is another machine (apache2), 888 is nextcloud with this instance of nginx (and php8.0) and I only use 88 for testing purposes so I don't use a subdomain for it) 2./ see EDIT 3./ yes I do – louis habert Nov 03 '21 at 19:56
  • EDIT : I could remove the port for nextcloud and maybe port 88 – louis habert Nov 03 '21 at 20:02
  • nginx requires either a location, an server name or an ip in the servername. else it will imho not serve the contents as it does not know whatever to do with it., moreover for what reason do you uae `~.*.` in servername? – djdomi Nov 04 '21 at 17:42
  • What is the content of nginx `error.log`? – Tero Kilkanen Nov 05 '21 at 08:04
  • @djdomi the regex is made to redirect every subdomain of otherdomain.fr to this ip – louis habert Nov 05 '21 at 09:01
  • @TeroKilkanen For 3 requests : otherdomain.fr domain.me:88 nextcloud.domain.me I get : `2021/11/05 10:05:52 [error] 22452#22452: *1 connect() failed (113: No route to host) while connecting to upstream, client: 172.70.90.59, server: ~.*.otherdomain.fr, request: "GET / HTTP/1.1", upstream$ 2021/11/05 10:05:55 [error] 22452#22452: *3 connect() failed (113: No route to host) while connecting to upstream, client: 172.70.162.73, server: ~.*.otherdomain.fr, request: "GET /favicon.ico HTTP/1.$` – louis habert Nov 05 '21 at 09:10

1 Answers1

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.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • i was guessing correctly that regrx works but is conflict with the rest ;) thabks fir the clarification of my thoughts – djdomi Nov 06 '21 at 14:43