I use reverse proxy with Nginx and I want to force the request into HTTPS, so if a user wants to access the url with http, he will be automatically redirected to HTTPS.
I'm also using a non-standard port.
Here is my nginx reverse proxy config:
server { listen 8001 ssl; ssl_certificate /home/xxx/server.crt; ssl_certificate_key /home/xxx/server.key; location / { proxy_pass https://localhost:8000; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; proxy_set_header X-Forwarded-Proto https; } }
I've tried many things and also read posts about it, including this serverfault question, but nothing has worked so far.
Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.
Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.
Nginx Redirect all HTTP traffic to HTTPS Open the Nginx configuration file for editing, then insert the following code: server { listen 80 default_server; server_name _; return 301 https://$host$request_uri; }
Nginx Redirect from HTTP to HTTPS (SSL) HTTP and HTTPS use different ports – HTTP port 80 and HTTPS port 443. Using HTTPS is much more helpful since it protects you from MITM attacks that can hijack your session. Remember, that for this method to work, you need to have an SSL already set up.
It can work as a reverse proxy or POP3/IMAP proxy. It is the third most popular web server and well known for its enhanced performance, ease of use and configuration, stability and minimum resource utilization. That’s why in this tutorial, we’ll show you how to use Nginx to redirect traffic in different ways.
The first one for the HTTP version of the site on port 80, and the other for the HTTPS version on port 443. To redirect a single website to HTTPS open the domain configuration file and make the following changes: listen 80 - The server block will listen for incoming connections on port 80 for the specified domain.
Found something that is working well :
server { listen 8001 ssl; ssl_certificate /home/xxx/server.crt; ssl_certificate_key /home/xxx/server.key; error_page 497 301 =307 https://$host:$server_port$request_uri; location /{ proxy_pass http://localhost:8000; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With