[英]Can't redirect https://www.example.com to https://example.com
I am using Nginx on Ubuntu and am able to redirect www.example.com to https://example.com but not from SSL to SSL with the www subdomain. 我在Ubuntu上使用Nginx,并且能够将www.example.com重定向到https://example.com,但不能将SSL重定向到带有www子域的SSL。
Here is my /etc/nginx/sites-enabled/myapp.conf
: 这是我的
/etc/nginx/sites-enabled/myapp.conf
:
# redirect non https traffic for the correct domains
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name www.example.com;
return 301 https://example.com$request_uri;
return 404; # managed by Certbot
}
server {
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# server for these domains
server_name example.com;
# first try to serve the erb version.
index index.html;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/myapp/code/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /usr/local/rvm/gems/ruby-2.5.1/wrappers/ruby;
}
Is there any way to redirect all www subdomain traffic to root domain, even when entering the www URL after https://? 即使在https://之后输入www URL,也可以将所有www子域流量重定向到根域吗?
Thank you, any help is appreciated. 谢谢您的帮助。
Yes, add the following server block: 是的,添加以下服务器块:
# remove 'www'
server {
listen 80;
listen 443 ssl;
server_name www.domain.com;
return 301 https://example.com$request_uri;
}
This is from the nginx docs . 这来自nginx docs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.