我需要添加www非www重定向到nginx配置。這裏是配置文件的一部分(似乎整個配置太長,堆棧溢出):Nginx的www非非www重定向
user nginx;
worker_processes 1;
error_log /var/deploy/mydomain/web_head/shared/log/nginx_error.log;
events
{
worker_connections 1024;
}
http
{
gzip on;
...
server {
server_name www.mydomain.com;
return 301 $scheme://mydomain.com$request_uri;
}
server
{
listen 80 default_server;
server_name _;
rails_env production;
passenger_enabled on;
root /var/deploy/mydomain/web_head/current/public;
client_max_body_size 50m;
# redirect to 503 if maintenance page present
if (-f $document_root/cloud66_maintenance.html)
{
return 503;
}
# redirect on errors
error_page 500 502 504 /50x.html;
error_page 503 @maintenance;
# handle error redirect
location = /50x.html
{
root html;
}
location @maintenance
{
error_page 405 = /cloud66_maintenance.html;
if (-f $document_root/cloud66_maintenance.html)
{
rewrite ^(.*)$ /cloud66_maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
try_files $uri /cloud66_maintenance.html @passenger;
location @passenger
{
passenger_enabled on;
passenger_min_instances 5;
passenger_set_cgi_param HTTP_X_FORWARDED_PROTO $scheme;
}
location ~ \.php$
{
deny all;
}
}
}
它沒有工作。我甚至嘗試添加以下塊到最後:
server {
server_name www.mydomain.com;
return 301 $scheme://mydomain.com$request_uri;
}
而且也試圖與server_name mydomain.com.
更換server_name _;
。
謝謝!
你重新加載nginx的設置?因爲你做得正確 –
是的,它已被重新加載。嗯,很奇怪。 –
是否正在訪問確切的'www.mydomain.com'?也ps:你不需要'server_name _;'如果你想匹配所有,你可以完全刪除它 –