Reputation: 767
I been struggling for a while to find a solution for a problem that I have: I need to redirect all http request to https excepting a specific page under a certain location. I have the following:
server {
listen 80;
listen 443 ssl;
server_name myserver;
root /var/www/example.org/htdocs;
index index.html;
.
.
}
I can't use:
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
.
.
}
I found a good solution in here
The problem with the solution is that the redirect gets into an infinitive loop, the solution is something like, after my changes:
map $uri $example_org_preferred_proto {
default "https";
~^/post/ "http";
}
server {
listen 80;
listen 443 ssl;
server_name example.org;
root /var/www/example.org/htdocs;
if ($example_org_preferred_proto = "https") {
return 301 $example_org_preferred_proto://example.org$request_uri;
}
}
I understand logic of the problem:
I get a request like: http://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test and got into a loop ....
I need a way to stop after redirect one time.
Upvotes: 0
Views: 228
Reputation: 6082
server {
listen 80;
server_name wwww.example.com;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
## here goes other ssl staff which you can find by link:
## https://gist.github.com/paskal/628882bee1948ef126dd
server_name example.org;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
Best solution is simplest one. You need to serve only one location and redirect anything else - so do it. If you have problem with using two server blocks, please describe it in detail.
Upvotes: 1