Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
share|improve this question

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...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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