0

I'm running a Debian server with Nginx installed, as well as OpenResty. I have a domain, a subdomain of that domain, and in the future will have multiple domains pointed at its IP address with A records.

I want an OpenResty server per domain or subdomain running on different ports, with an Nginx server routing requests between the servers depending on domain name.

So, right now I have:

  • domain1.com and sub.domain1.com pointing at the IP
  • Nginx running on port 80 and routing requests (config changes I made explained below)
  • domain1.com is being served on port 8000, sub.domain1 is being served on port 8001

I want:

  • Nginx will make it appear to the user that they are accessing sub.domain1.com or domain1.com. No domain1.com:8000 or sub.domain1.com:8001
  • In the future, when I have domain2.com pointed at the IP, and a server running on port 8002, it also appears to the user as domain2.com instead of domain2.com:8002, and so on

I have tried using proxy_pass, proxy_set_header, proxy_redirect, in various configurations as suggested by searching with Google. I managed to once have a proxy_pass working, but that showed the port to the user. The whole reason I'm trying to set things up this way is so that it doesn't appear to be running on multiple ports to the user.

Additionally, I am using SSL, and want HTTP requests to redirect to HTTPS. I realized while trying to figure this out I need to change the proxy server to port 443 for SSL requests (to be running on the default port).

This is how I got it working with the subdomain:

Nginx's config (/etc/nginx/nginx.conf) is the default with the following exceptions:

  1. Removed the Virtual Host include directives:

    #include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;
    
  2. Added the certificate for domain1.com in the http { } block:

    ssl_certificate /path/to/public.pem;
    ssl_certificate_key /path/to/private.pem;
    
  3. Defined the following for my proxy:

    server {
        listen 443 ssl;
        ssl on;
        server_name dev.domain1.com;
        location / {
            proxy_pass https://sub.domain1.com:8001;
        }
    }
    
    server {
        listen 80;
        server_name sub.domain1.com;
        return 301 https://$host$request_uri;
    }
    

This worked just fine, but then I tried the same server { } blocks for the domain itself, that worked just fine and this broke.

What the hell am I doing?

Additional notes:

The servers being proxied are essentially:

http {
    ssl_certificate /path/to/public.pem;
    ssl_certificate_ley /path/to/private.pem;

    include mime.types;

    server {
        listen port ssl;
        ssl on;
        error_page 497 https://$host:$server_port$request_uri;

        location / {
            #whatever
        }
    }
}
1
2

The first thing you need to do is define what requests you want nginx to listen for, which you do with server_name and also define which port you want to listen on using listen.

From what you have said it sounds like you simply want people to enter domain1.com into their browser and then the content to be served. If that is the case this will do that, with Nginx listening for requests on port 80 and passing those to OpenResty on port 8000/8001/800x.

However these are very basic configs that will do exactly that but you may need more settings. Review the documents here and here for a full list including further ssl settings

server { 
listen          80;
server_name     domain1.com;

location / {
  proxy_pass      http://127.0.0.1:8000;
    }
}  

server { 
listen          80;
server_name     domain2.com;

location / {
  proxy_pass      http://127.0.0.1:8001;
    }
}

Personally I prefer to manage/organise the sites using the /etc/nginx/sites-enabled/ folder but "can" put this in the nginx.con inside the http block.

Otherwise you would add back the lines you removed, split these configs into two files and then place those files in /etc/nginx/sites-available and symlink to the /etc/nginx/sites-enabled folder.

9
  • Make sure OpenResty is listening as expected on 8000 and 8001 and on 127.0.0.1 – Drifter104 Mar 1 '16 at 22:40
  • This is essentially what I had, except in my proxy_pass directives, I was using the domain name with port instead of IP. I'll try this instead and see how that goes. – Guard13007 Mar 2 '16 at 0:01
  • This led me exactly to the problem I had before. The URL is now the IP address when connecting instead of the domain. – Guard13007 Mar 2 '16 at 0:21
  • @Guard13007, what? This is the basic config used by thousands of servers. Check if your app send wrong redirect. Use e.g. curl -v http://domain1.com and look for redirects – Alexey Ten Mar 2 '16 at 5:50
  • I realized while working on this further that I really wasn't clear enough with what I was trying to do, and edited the question to provide corrected details and more information. I have part of it working, but one part breaks the other part. – Guard13007 Mar 2 '16 at 17:04
0

I believe you need to setup the listen directive for each virtual host server {} block, or domain name, you're serving.

http://nginx.org/en/docs/http/ngx_http_core_module.html#listen

server {
    listen domain1.com:8000;
    ...
}
server {
    listen domain2.com:8001;
    ...
}    

You also might need to open those ports if using a firewall.

If you have a default setup of Nginx, then you probably need to comment out any listen directives that might be loaded in conf files and restart nginx.

1
  • Each OpenResty server is already configured to listen on the appropriate port. My problem is how to proxy a connection on port 80 to go to those ports without the URL changing. – Guard13007 Mar 2 '16 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.