0

This may have been answered many times before but most answers are site specific so wanted some insight on a bareboned nginx config on how to redirect multiple external sites under same server but different subdomain. Pls note the external sites are inaccessible and need the reverse proxy via XYZ to make accessible.

I found existing nginx config already a proxy set up for site1 : AAA proxied through http://XYZ:8088. Below is the existing config. Now, how do I go about adding another site2 to be proxied via http://XYZ:8088/site2

So far, I tried to add additional section at the bottom of config similar to site1 (which is perfectly working fine), however site2 css/images is lost if I try to hit http://XYZ:8088/site2

server {
    listen      8088 default_server;
    server_name "";
    return      444;
}

server {
    listen           8088;
    server_name      "XYZ.example.com";
    charset          utf-8;

    # Deny access to .htaccess files.
    location ~ /\.ht {
        deny all;
    }

    # Proxy to site1 server.
    location / {
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name:$server_port;
        proxy_pass http://site1;
    }

    # Proxy to site2 server.
    location /site2 {
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://site2/;
    }
}

upstream site1 {
    server site1:1111;
}

upstream site2 {
    server site2:2222;
}

Any help would be appreciated. Also if someone can explain why below config is behaving this way ? I understand it has something to do with the additional "/site2" being used now. But how do I make it ignore that.

StrangerThinks
  • 246
  • 4
  • 14
  • 1
    Because when the HTML from the second site gets loaded into the browser, it has all the assets links inside written somewhat like ``. Next, when the browser tries to load the CSS, it makes a request to your `XYZ.site.com` server as `GET /css/style.css HTTP/1.1`, and how do you think, what location will be chosen by nginx to handle this request? Isn't it obvious? – Ivan Shatsky May 17 '22 at 19:39
  • Thanks for the explanation. What is the workaround ? or a way to let nginx redirect it to upstream ? – StrangerThinks May 17 '22 at 19:46
  • Unfortunately by the nature of this problem there are no reliable workaround unless you can change the way the proxied site generates links for its assets. Read [this](https://stackoverflow.com/a/62840133/7121513) answer. – Ivan Shatsky May 17 '22 at 20:11
  • How does site1 work? since that is also inaccessible but yet loads all the css. What is different about site1 ? Can similar approach be used for site2 ? – StrangerThinks May 18 '22 at 12:15
  • I thought you've understand my explanations from the first comment. Didn't you see the difference between `location / { ... }` and `location /site2 { ... }`? Or maybe you don't understand how nginx choose a location to handle the request? Read the `location` directive documentation first. Put your first site under `location /site1 { ... }` and it will stop loading any assets too. – Ivan Shatsky May 18 '22 at 12:22
  • That is, to not break any URI reverse proxied by nginx, your only way is to use different subdomains for each proxied site, e.g. `site1.XYZ.example.com`, `site2.XYZ.example.com`, etc. – Ivan Shatsky May 18 '22 at 12:24
  • Is using a different port instead of 8088 possible to proxy similar to site1 ? How would the snippet look like? – StrangerThinks May 18 '22 at 12:36
  • Try multiple `server` blocks each using its own `listen ` directive and only one `location / { ... }` for each of the proxied sites, that could work. – Ivan Shatsky May 18 '22 at 12:40

0 Answers0