我需要将www添加到非www重定向到nginx配置。这是配置文件的一部分(看起来整个配置对于堆栈溢出来说太长了):
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 _;
替换为server_name mydomain.com.
。
谢谢!
答案 0 :(得分:0)
如果您使用CustomConfig,Cloud 66会自动重新加载配置。你可以尝试这个:
if ($http_host = www.mydomain.com) {
rewrite (.*) http://mycomain.com$1 permanent;
}
这可以在您的server
部分
答案 1 :(得分:0)
我最终使用了简单的网址转发服务:https://dnsimple.com/url-forwarding-301-redirect。无需编辑nginx配置即可使用。
答案 2 :(得分:0)
在配置文件的末尾添加新的服务器块。
server {
listen 80;
server_name www.your-domain.com;
return 301 $scheme://your-domain.com$request_uri;
}
A detailed tutorial on enabling www redirect with nginx can be found here