R
R
Roma2019-01-14 12:33:10
Nginx
Roma, 2019-01-14 12:33:10

How to make a redirect from httpS to http of only one page in nginx?

There are 2 servers and 1 ip, on one nginx server, on another apache2 and several sites
I made this config for proxying with nginx on apache, installed let's encrypt and everything works fine

proxy.conf

server {
    if ($host = kp.site1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = site1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = site3.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = site2.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen            80;
        server_name       site1.com site2.com kp.site1.com site3.com;

        access_log       /var/log/nginx/ssl-access.log;

        location / {
                proxy_pass            http://10.10.10.20/;
                proxy_redirect        off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_pass_header Set-Cookie;
        }
}

server {
        listen            443;
        server_name       site1.com site2.com kp.site1.com site3.com;

        access_log       /var/log/nginx/ssl-access.log;

        location / {
                proxy_pass            http://10.10.10.20/;
                proxy_redirect        off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_pass_header Set-Cookie;
        }
    ssl_certificate /etc/letsencrypt/live/site2.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site2.com/privkey.pem; # managed by Certbot
}

but you need to serve one specific page site1.com/blabla/bla.php via http, how to do it?

addition

location = https://site1.com/blabla/bla.php {
    root http://site1.com/blabla/bla.php;
  }


doesn't work or am I doing something wrong

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roma, 2019-01-14
@aaallllsss

replace the redirect of everything

if ($host = site1.com) {
     return 301 https://$host$request_uri;
}

on the
if ($request_uri = /) {
  return 301 https://$host$request_uri;
}

enable redirect from https://site1.com/blabla/bla.php to site1.com/blabla/bla.php
if ($request_uri = /blabla/bla.php) {
  return 301 http://$host$request_uri;
}

final config for site1.com
server {
  if ($request_uri = /) {
     return 301 https://$host$request_uri;
    }


        listen            80;
        server_name       site1.com;

        access_log       /var/log/nginx/http-access.log;
    error_log		 /var/log/nginx/http-error.log;

        location / {
                proxy_pass            http://10.10.10.20;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
        }
}

server {
  if ($request_uri = /blabla/bla.php) {
     return 301 http://$host$request_uri;
    }
        listen            443;
        server_name       site1.com;

        access_log       /var/log/nginx/https-access.log;
    error_log		 /var/log/nginx/https-error.log;

        location / {
                proxy_pass            http://10.10.10.20;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
        }
    ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem; # managed by Certbot
}

A
Alexander Chernykh, 2019-01-14
@sashkets

look at http on https
I think the idea will become clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question