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
andsub.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
ordomain1.com
. Nodomain1.com:8000
orsub.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 asdomain2.com
instead ofdomain2.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:
Removed the Virtual Host include directives:
#include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*;
Added the certificate for
domain1.com
in thehttp { }
block:ssl_certificate /path/to/public.pem; ssl_certificate_key /path/to/private.pem;
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
}
}
}