I have a node application running on port 8443. My nginx handles the web requests on port 80 and 443 and should redirect the user to 8443.
here's my /etc/nginx/sites-enabled/default
config:
upstream my_upstream {
server 127.0.0.1:8443;
keepalive 64;
}
server {
listen 80;
server_name myapp.com;
rewrite ^/(.*) https://myapp.com/$1 permanent;
}
server {
listen 443 ssl;
server_name 12.34.12.34 www.myapp.com myapp.com *.myapp.com;
ssl_certificate /path/to/my/cert.crt;
ssl_certificate_key /path/to/my/private.key
// other ssl params
location / {
proxy_redirect off;
proxy_pass https://my_upstream;
// other params
}
}
with this config I can access my app via
http(s)://myapp.com:8443
only when I add the following iptables
iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
iptables -t nat -A OUTPUT -p tcp --dport 443 -o lo -j REDIRECT --to-port 8443
I can access
http(s)://myapp.com
Questions:
It seems kinda silly to me to redirect from port 80 to 443 to 8443 using iptables. Is there a way to do this with nginx only (or is this the way to go?).
Is this approach (having the application on a non standard port like 8443) a good idea at all?