In my nginx config, I intended to reach purposes below:
- forward www to non-www
- forward https to http
- only use https on uri with pattern 'secure'
Code below reach the goals, except that at point 3, Chrome shows blank page with error 'This webpage has a redirect loop'. Anybody can tell proper config for this, and/or any better code?
#redirect www to non-www
server {
listen 80;
server_name www.example.org;
return 301 http://example.org$request_uri;
}
#redirect https to http, for both www and non-www
#but process https for uri with pattern 'secure'
server {
ssl_certificate /home/mydomain/ssl.cert;
ssl_certificate_key /home/mydomain/ssl.key;
listen 443 ssl;
server_name www.example.org example.org;
root /home/mydomain/public_html/mydomain/public;
index index.php;
access_log /var/log/virtualmin/example.org_access_log;
error_log /var/log/virtualmin/example.org_error_log;
#if has pattern 'secure', just process
#---problem is here, chrome result 'This webpage has a redirect loop'---
location ~ ^/(secure) {
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
return 301 http://example.org$request_uri;
}
server {
listen 80;
server_name example.org;
root /home/mydomain/public_html/mydomain/public;
index index.php;
access_log /var/log/virtualmin/example.org_access_log;
error_log /var/log/virtualmin/example.org_error_log;
#rewrite to https if has pattern secure
location ~ ^/(secure) {
#rewrite ^(.*) https://$host$1 permanent;
return 301 https://example.org$request_uri;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
http
andhttps
?