Score:0

502 Bad Gateway when using NginX proxy_pass for subdomain on different LAN server

my flag

In a nutshell, I want to host a second domain (sub.domain.com) on a different server, on the same network (192.168.1.240).

I understand I have to make use of the proxy_pass, so I have this configuration on the first server (/etc/nginx/sites-available/sub.domain.com):

server {
    server_name sub.domain.com;
    location / {
        proxy_pass https://192.168.1.240;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/sub.domain.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
}
server {
    if ($host = sub.domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
listen 80;
    server_name sub.domain.com;
    return 404; # managed by Certbot
}

And on the second server this (/etc/nginx/sites-available/default):

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:10m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
server {
location ^~ /adminer/.*\.php$ {
        return 444;
        allow 192.168.1.0/24;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
    listen 80 default_server;
    listen [::]:80 default_server;
#listen 443 ssl;
    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

#   server_name _;
server_name sub.domain.com;
    set $skip_cache 0;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }   
    if ($query_string != "") {
        set $skip_cache 1;
    }   

    # Don't cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }   

    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

    location / {
       try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 60m;    
}
}

For the life of me, I cannot make it work. Visiting sub.domain.com in the browser shows a 502 Bad Gateway error and I'm out of ideas.

The Nginx error logs displays this error:

*13 connect() failed (111: Unknown error) while connecting to upstream

Anyone sees anything wrong with my config, or what I'm trying to do is not even possible?

If it matters, domain.com works just fine on the first server, which is Ubuntu with PHP7.4 and obviously nginx. Second (where sub.domain.com I'd like to be) is Raspberry Pi 4, PHP 8.2, nginx.

Also, visiting http://192.168.1.240/ loads the site, although it doesn't load the CSS/JS/IMG resources which are linked to sub.domain.com.

Johnny Bravo avatar
my flag
I believe the issue is because the original server has the letsencrypt ssl keys, and the second one server the site as http. I can't find a way to install the ssl certificate on the second, probably because it is behind the first's nginx proxy.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.