Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite single page https to http Nginx

I would like to rewrite 'https://www.example.com' and 'https://example.com' to 'http://www.example.com' or 'http://example.com respectively'. I only want to write that specific part of the site not any subfolders or php related pages. How do I do this?

like image 989
Sahil Sinha Avatar asked Feb 25 '23 01:02

Sahil Sinha


2 Answers

I use it in the other direction (not tested in this direction but should work).

if ( $scheme = https )
{
  rewrite ^ http://$host$uri;
}

EDIT: limit to a location and end don't try to rewrite more:

location / {
  if ( $scheme = https )
  {
    rewrite ^ http://$host$uri last;
  }
}
like image 81
shellholic Avatar answered Mar 05 '23 16:03

shellholic


I've used something similar to the following, which avoid's IfIsEvil and uses return instead of rewrite . However it fails DRY, which bothers me.. Suggestions welcome.

server {
    listen 80;
    server_name .example.com;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php5-fpm.sock ;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen              443 ssl;
    server_name         .example.com;
    ssl_certificate     /etc/ssl/certs/example.com.cert;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    index index.html index.htm index.php;

    # Rewrite only / to http
    location = / {
        return 301 http://$server_name$request_uri;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php5-fpm.sock ;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

See also

  • nginx http location docs
  • related nginx https to http answer
like image 32
here Avatar answered Mar 05 '23 17:03

here