This is what I do:

Because /api system has problem with HTTPS, I have to serve it under HTTP. The rest I force them to be in HTTPS. However, in my django system, redirects gives me back http. It may have to do with django, but for now, I want to rewrite all HTTP to HTTPs. I normally would put that inside the 80 server block, but I want to keep /api as HTTP.

How can I do that using rewrite?


    server {
        listen 80;
        server_name localhost;
        server_name_in_redirect off;
        client_max_body_size 100M;

        location /api {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass http://localhost:5050;
            proxy_redirect default;

    server {

        listen 443;
        ssl on;

        location /app1 {
         proxy_pass http://localhost:1234;

        location /app2 {
         proxy_pass http://localhost:5678;

Ah. I just tried something and it looks like it's working.

if ($request_uri !~ (/api)){
    rewrite     ^(.*)   https://$http_host$1 permanent;

and then leave everything else in the 80 block intact... Not gonna check my answer off...

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.