search

Home  >  Q&A  >  body text

rewrite - nginx implements https for special urls

The system with an interface all uses http requests. Now I like to use ssl for URLs with login passwd. For example, when the app calls os.test.com/a/p/login, it uses https requests but other requests. I still use http requests. Now I use openssl to configure the certificate private key, etc. But now every request uses https requests, such as
server {

listen      443;
server_name  os.test.com;
ssl on;
ssl_certificate /etc/ngx/conf.d/server.crt;
ssl_certificate_key /etc/ngx/conf.d/server.key;
location / {
    proxy_pass http://127.0.0.1:9988;
}

}
How should I modify this

仅有的幸福仅有的幸福2748 days ago600

reply all(2)I'll reply

  • 阿神

    阿神2017-05-16 17:23:05

    You have to write it separately. First create a server to specifically handle https requests, and then reverse proxy it based on the URL. Here is the code:

    Handle non-https requests, https requests are reverse-proxyed out

    server {

    listen      443;
    server_name  os.test.com;
    location / {
    
    }
    
    location ~* .(login|passwd)$ {
        proxy_pass http://127.0.0.1:9999
    }

    }

    Handling https requests

    server{

    listen 9999
    server_name 127.0.0.1
    ssl on;
    ssl_certificate /etc/ngx/conf.d/server.crt;
    ssl_certificate_key /etc/ngx/conf.d/server.key;
    location / {
     
    }

    }

    reply
    0
  • 黄舟

    黄舟2017-05-16 17:23:05

    # http
    server {
        listen 80;
        
        location /login {
            rewrite ^  https://$host$request_uri permanent;
        }
    }
    
    # https
    server {
        listen 443;
    }

    reply
    0
  • Cancelreply