Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect on the same port from http to https with nginx reverse proxy

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.

like image 926
rbinsztock Avatar asked Mar 15 '13 09:03

rbinsztock


People also ask

How do I redirect traffic from HTTP to HTTPS in nginx?

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.

What is return 301 in nginx?

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.

How do I redirect HTTP to HTTPS in Nginx?

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; }

What ports does Nginx use for redirects?

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.

Can Nginx be used as a reverse proxy?

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.

How do I redirect a single website to https?

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.


1 Answers

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;         } } 
like image 135
rbinsztock Avatar answered Sep 22 '22 12:09

rbinsztock