banri16
banri16

Reputation: 257

Nginx Reverse proxy force https

We have a server setup for reverse proxy using nginx. This server will redirect visitors to the corresponding server in which path they are accessed. Now, I want to set up the nginx.conf to force redirect them to https. The problem is I am getting a "too many redirection" error. I tried using a rewrite and adding "proxy_set_header X-Forwarded-Proto https;" but none of it works. Is there a way we can achieve this? We have set up a ssl cert on cloudflare so we won't need to add it on the config.

Below is the config setting of the current nginx.

   server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

   if ($http_x_forwarded_proto = "http") {
      rewrite  ^/(.*)$  https://development-link/$1 permanent;
   }

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://development-elb1;
            proxy_set_header X-Forwarded-Proto https;
    }

    location /en {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://development-elb2;
            proxy_set_header    X-Forwarded-Proto https;
    }

Upvotes: 1

Views: 10919

Answers (2)

Satys
Satys

Reputation: 2435

So you are using cloudflare as your DNS.

There is a very easy hack to force the users to https even if you are using free version of cloudflare at the cloudflare end. And just host http version at your Nginx.

1- Open cloudflare and choose your domain.

2- Go to Page Rules tab.

3- Create page rule

4- for the url, put http://www.example.com/*

5- for setting, choose always use https

6- Click Save and deploy

Now you have configured at your cloudflare to force user to redirect to https url.

Upvotes: 0

Tan Hong Tat
Tan Hong Tat

Reputation: 6879

Use a separate block.

# Redirect HTTP to HTTPS
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    return 301   https://$host$request_uri;
}

# HTTPS
server {
    listen       443 default_server;
    listen       [::]:443 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;       

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://development-elb1;
        proxy_set_header X-Forwarded-Proto https;
    }

    location /en {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://development-elb2;
        proxy_set_header    X-Forwarded-Proto https;
    }
}

Upvotes: 6

Related Questions