Welcome! Log In Create A New Profile

Advanced

Trailing slash madness

Posted by cyberfarer 
Trailing slash madness
April 07, 2016 10:05AM
I have Googled and researched and I have recreated my conf file so often and I can't get past this issue with the trailing slash. I really hoping someone can help before I give up and try Apache.

We have a firewall in front of our server so we are allowing HTTPS only. There is no HTTP access.

My conf file:

server {
listen 443;
server_name sub.domain.com;
server_name_in_redirect on;

access_log off;
error_log /var/log/nginx/error.log crit;

ssl on;
ssl_certificate /etc/ssl/cert/cert.crt;
ssl_certificate_key /etc/ssl/cert/cert.key;

ssl_session_timeout 5m;

ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;

location / {

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_set_header X-Forwarded-Proto $scheme;

proxy_pass http://192.168.10.202;

proxy_redirect http://192.168.10.202 https:/sub.domain.com;
}
}

The last line is recent.

Here are the results. With a trailing slash:

curl -I https://sub.domain.com/test/
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 07 Apr 2016 14:01:08 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14

Without a trailing slash:

curl -I https://sub.domain.com/test
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 07 Apr 2016 14:02:16 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://sub.domain.com/test/

Please note that it introduces a permanent redirect to HTTP rather than HTTPS. Why? How do I stop this behaviour?

I have spent a day on this and there appears to be no single and effective way of solving this issue.

I would be very grateful of any help.

Thanks.



Edited 2 time(s). Last edit at 04/07/2016 10:06AM by cyberfarer.
Re: Trailing slash madness
April 07, 2016 01:27PM
I have it working.

The solution is quite common but hasn't worked previously. Here is the updated conf file:

server {
listen 443;
server_name sub.domain.com;
server_name_in_redirect on;

access_log off;
error_log /var/log/nginx/error.log crit;

ssl on;
ssl_certificate /etc/ssl/cert/cert.crt;
ssl_certificate_key /etc/ssl/cert/cert.key;

ssl_session_timeout 5m;

ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;

rewrite ^([^.]*[^/])$ $1/ permanent;
location / {

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_set_header X-Forwarded-Proto $scheme;

proxy_pass http://192.168.10.202;

proxy_redirect http://192.168.10.202 https://sub.domain.com;
}
}

What's different is I added the line: rewrite ^([^.]*[^/])$ $1/ permanent;

That little bit of text is readily found by searching Google but what isn't clear is where it should go in the file.

It didn't work for me until I placed it immediately ahead of the location {} section.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 177
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready