[英]Nginx www to non-www redirect
I need to add www to non-www redirect to nginx config. 我需要将www添加到非www重定向到nginx配置。 Here's the part of the config file (seems like whole config is too long for stack overflow): 这是配置文件的一部分(似乎整个配置对于堆栈溢出来说太长了):
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;
}
}
}
It didn't work. 没用 I even tried adding following block to the end: 我什至尝试将以下代码块添加到末尾:
server {
server_name www.mydomain.com;
return 301 $scheme://mydomain.com$request_uri;
}
And also tried replacing server_name _;
并且还尝试替换server_name _;
with server_name mydomain.com.
使用server_name mydomain.com.
. 。
Thanks! 谢谢!
Cloud 66 automatically reloads the configuration if you are using CustomConfig. 如果您正在使用CustomConfig,则Cloud 66会自动重新加载配置。 You can try this one: 您可以尝试以下一种方法:
if ($http_host = www.mydomain.com) {
rewrite (.*) http://mycomain.com$1 permanent;
}
This can be under your server
section 这可以在您的server
部分下
I ended up using dnsimple url forwarding service: https://dnsimple.com/url-forwarding-301-redirect . 我最终使用了dnsimple网址转发服务: https ://dnsimple.com/url-forwarding-301-redirect。 Works great without the need to edit nginx config. 无需编辑nginx配置,效果很好。
Add a new server block at the end of your configuration file. 在配置文件的末尾添加一个新的服务器块。
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 在此处可以找到有关使用Nginx启用www重定向的详细教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.