我在Rackspace云上使用nginx,遵循一个教程,搜索了网络,到目前为止还不能得到这个排序。
我想要www.mysite.example去mysite。例如,正常的。htaccess为SEO和其他原因。
My /etc/nginx/sites-available/www.example.com.vhost config:
server {
listen 80;
server_name www.example.com example.com;
root /var/www/www.example.com/web;
if ($http_host != "www.example.com") {
rewrite ^ http://example.com$request_uri permanent;
}
我也试过
server {
listen 80;
server_name example.com;
root /var/www/www.example.com/web;
if ($http_host != "www.example.com") {
rewrite ^ http://example.com$request_uri permanent;
}
我也试过。第二次尝试都给出重定向循环错误。
if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}
我的DNS设置为标准:
site.example 192.192.6.8 A type at 300 seconds
www.site.example 192.192.6.8 A type at 300 seconds
(示例ip和文件夹已用于示例和帮助人们在未来)。我使用Ubuntu 11。
HTTP的解决方案
从文档中可以看出,“正确的方法是为example.org定义一个单独的服务器”:
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
...
}
HTTPS的解决方案
对于那些想要解决方案的人,包括https://..。
server {
listen 80;
server_name www.domain.example;
# $scheme will get the http protocol
# and 301 is best practice for tablet, phone, desktop and seo
return 301 $scheme://domain.example$request_uri;
}
server {
listen 80;
server_name domain.example;
# here goes the rest of your config file
# example
location / {
rewrite ^/cp/login?$ /cp/login.php last;
# etc etc...
}
}
注意:我最初没有在我的解决方案中包括https://,因为我们使用loadbalancer和我们的https://服务器是一个高流量SSL支付服务器:我们不混合https://和http://.
检查Nginx版本,使用Nginx -v。
用Nginx重定向从URL中剥离www
server {
server_name www.domain.example;
rewrite ^(.*) http://domain.example$1 permanent;
}
server {
server_name domain.example;
#The rest of your configuration goes here#
}
所以你需要有两个服务器代码。
使用Nginx重定向将www添加到URL中
如果你需要的是相反的,从域重定向。www.domain.example的例子,你可以使用这个:
server {
server_name domain.example;
rewrite ^(.*) http://www.domain.example$1 permanent;
}
server {
server_name www.domain.example;
#The rest of your configuration goes here#
}
正如您可以想象的那样,这与第一个示例正好相反,其工作方式与第一个示例相同。这样,你不会得到SEO标记下来,因为它是完全的烫发重定向和移动。没有WWW是强制的,目录显示!
我的一些代码如下所示,以便更好地查看:
server {
server_name www.google.com;
rewrite ^(.*) http://google.com$1 permanent;
}
server {
listen 80;
server_name google.com;
index index.php index.html;
####
# now pull the site from one directory #
root /var/www/www.google.com/web;
# done #
location = /favicon.ico {
log_not_found off;
access_log off;
}
}
我的配置是- Nginx + tomcat 9 + Ubuntu 20.04 + spring boot app
上面所有的答案都不适合我-也不适合Nginx文件中的上游符号-所以我改变了我的设置
感谢上帝的certbot -这个util非常有用,它为您的网站生成基本文件,然后我添加了我的更改-重定向https://www.example.com, http://www.example.com到只有一个https://example.com
server {
if ($host = www.example.com) {
return 301 https://example.com$request_uri;
}
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8080; # This is upstream name, note the variable $scheme in it
proxy_redirect off;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://example.com$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}