添加SSL后,我注意到一个奇怪的行为:地址中使用HTTP协议的所有API POST请求都被转换为GET请求,这就是我收到ActionController::RoutingError (No route matches [GET] "/api/v2/users")
错误的原因。
我的nginx.conf:
upstream unicorn_staging.xxxxx.com {
server unix:/srv/www/xxxxx/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name staging.xxxxx.com xxxxx dublin;
access_log /var/log/nginx/staging.xxxxx.com.access.log;
keepalive_timeout 5;
root /srv/www/xxxxx/current/public/;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_staging.xxxxx.com;
break;
}
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /srv/www/xxxxx/current/public/;
}
}
server {
listen 443;
server_name staging.xxxxx.com xxxxx dublin;
access_log /var/log/nginx/staging.xxxxx.com-ssl.access.log;
ssl on;
ssl_certificate /etc/nginx/ssl/staging.xxxxx.com.crt;
ssl_certificate_key /etc/nginx/ssl/staging.xxxxx.com.key;
keepalive_timeout 5;
root /srv/www/xxxxx/current/public/;
location / {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_staging.xxxxx.com;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /srv/www/xxxxx/current/public/;
}
}
我正在使用Ruby on Rails,Nginx和unicorn。我在rails中将force_ssl设置为true,因此它会自动将所有请求重定向到https。 使用https时访问日志:
[01/Apr/2016:08:58:28 +0000] "GET /api/v2/users HTTP/1.1" 404 15384 "-" "XxxxStaging/28 CFNetwork/758.2.8 Darwin/15.0.0"
使用http:
时访问日志 [01/Apr/2016:09:00:08 +0000] "POST /api/v2/users HTTP/1.1" 201 1620 "-" "curl/7.43.0"