Unable to rewrite URL in Nginx when using SSL on non-standard ports
I am using a test instance of my application on non-standard ports and would like to use nginx to redirect all incoming http
requests to https
Here is my nginx config:
I tried the above but Cant seem to get it working : please see below
......
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 9000;
server_name iossapp1.com;
if ($scheme = 'http') {
rewrite ^ https://$server_name:9443$request_uri? permanent;
}
}
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
server {
listen 9443 ssl;
server_name iossapp1.com;
error_page 497 https://$host:9443$request_uri;
location / {
proxy_pass http://127.0.0.1:9001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
I have referenced the following URL but it dosent seem to help : http://forum.nginx.org/read.php?2,155978,213340#msg-213340
To add to the issue: the problem only surfaces when I POST a form to the server, here are a few of examples from the /var/log/nginx/access.log file :
127.0.0.1 - - [07/Jan/2013:17:29:14 +0800] "GET /admin/logout HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/iosIndex" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
127.0.0.1 - - [07/Jan/2013:17:32:45 +0800] "POST /admin/editStaff HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/editStaff?staff.id=612" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
127.0.0.1 - - [07/Jan/2013:17:32:45 +0800] "GET /admin/staffIndex HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
127.0.0.1 - - [07/Jan/2013:17:35:45 +0800] "POST /admin/authenticate HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
127.0.0.1 - - [07/Jan/2013:17:35:45 +0800] "GET /admin/masterIndex HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
In contrast a GET URL behaves correctly like so :
127.0.0.1 - - [07/Jan/2013:17:36:21 +0800] "GET /admin/masterIndex HTTP/1.1" 200 2324 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
I noticed the numerical value after the response code (302, 304) that is 0 when the redirect is going to be unsuccessful, in the cases where it works, the numerical value is non-zero. What does this number represent?
I found the solution:
My entry for
proxy_set_header Host $host:$server_port;
was originally missing the $server_port
Source : https://stackoverflow.com/questions/10168155/nginx-proxy-https-to-http-on-non-standard-port
Now it works! =D