Question
force SSL on one page, non-SSL on others
How do I set up Nginx conf file to force SSL on only one of the page in my site and non-SSL on all the rest?
For example, I want all of the URLs under /login to be https but all the rest of the URLs to be http.
from another place i found that i need to write these following codes.
for non ssl server block
server {
root /var/www/
location / {
}
location /login {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
and for ssl block the opposite
server {
listen 443;
root /var/www/
location / {
rewrite ^ http://$http_host$request_uri? permanent;
}
location /login {
}
}
but i tried to implement above codes in my server block but no luck. either it gives syntax error or redirect loop. i just dont understand where exactly i must put those codes.
here is my server block configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.php index.html index.htm;
server_name mysite.com www.mysite.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~* .(jpg|jpeg|png|gif|woff|ico|css|js)$ {
expires 365d;
}
}
server {
listen 443 default_server spdy;
listen [::]:443 default_server spdy;
root /var/www/;
index index.php index.html index.htm;
server_name mysite.com www.mysite.com;
ssl on;
ssl_certificate /my-ssl.crt;
ssl_certificate_key /mysite.com.key;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~* .(jpg|jpeg|png|gif|woff|ico|css|js)$ {
expires 365d;
}
}
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×