Nginx重定向到www
再次出现nginx重定向问题。我需要我的网站只能通过https访问,并始终与子域www(或应用程序或api)。有没有办法接受以下配置(不带www)?因为这就是我现在看到的,无法解释为什么。。。我只是补充说,nginx配置中没有其他服务器部分Nginx重定向到www,nginx,Nginx,再次出现nginx重定向问题。我需要我的网站只能通过https访问,并始终与子域www(或应用程序或api)。有没有办法接受以下配置(不带www)?因为这就是我现在看到的,无法解释为什么。。。我只是补充说,nginx配置中没有其他服务器部分 server { listen 80; server_name ~^(?<subdomain>www|app|api)\.example\.com$; index index.html index.htm; ret
server {
listen 80;
server_name ~^(?<subdomain>www|app|api)\.example\.com$;
index index.html index.htm;
return 301 https://$subdomain.example.com$request_uri;
}
server {
listen 443 ssl;
server_name ~^(?<subdomain>www|app|api)\.example\.com$;
root /var/www/html/pathToMyWebsite;
index index.php;
}
服务器{
听80;
服务器名称^(?www | app | api)\.example\.com$;
index.html index.htm;
返回301 https://$subdomain.example.com$request\u uri;
}
服务器{
听443ssl;
服务器名称^(?www | app | api)\.example\.com$;
root/var/www/html/pathToMyWebsite;
index.php;
}
编辑:这是我最后使用的东西,感谢@ivan:
# Redirects http://example.com & https://example.com to https://www.example.com
server {
listen 80;
listen 443 ssl; # Here is the trick - listen both 80 & 443.
# other ssl related stuff but without "ssl on;" line.
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
server_name ~^(?<subdomain>www|app|api)\.example\.com$;
index index.html index.htm;
return 301 https://$subdomain.example.com$request_uri;
}
server {
listen 443 default_server ssl;
server_name ~^(?<subdomain>www|app|api)\.example\.com$;
root /var/www/html/pathToMyWebsite;
index index.php;
}
#重定向http://example.com & https://example.com 到https://www.example.com
服务器{
听80;
听443ssl;#这里是诀窍-同时听80和443。
#其他ssl相关的东西,但没有“ssl在线”。
server_name example.com;
返回301https://www.example.com$request_uri;
}
服务器{
听80;
服务器名称^(?www | app | api)\.example\.com$;
index.html index.htm;
返回301 https://$subdomain.example.com$request\u uri;
}
服务器{
侦听443默认_服务器ssl;
服务器名称^(?www | app | api)\.example\.com$;
root/var/www/html/pathToMyWebsite;
index.php;
}
注意我在
listen443 default\u server ssl上添加的“default\u server”代码>首先,在nginx配置中使用regexp通常不是一个好的解决方案。几乎总是复制和粘贴配置块或使用include
语句更合理,因为regexp引入了冗余的复杂性。当然,这是一种对使用什么的权衡
但是,我在配置中使用了非常相同的使用模式:
# Redirects http://example.com & https://example.com to https://www.example.com
server {
listen 80;
listen 443 ssl; # Here is the trick - listen both 80 & 443.
# other ssl related stuff but without "ssl on;" line.
server_name example.com;
return 301 https://www.example.com$request_uri;
}
# Redirects http://*.example.com to https://*.example.com
server {
listen 80;
server_name ~^.+\.example\.com$;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name ~^.+\.example\.com$;
ssl on;
# ...
}
我最终使用了你和我的混合解决方案。我不得不把listendefault_服务器443代码>因为我有此错误,所以在ssl握手时在侦听ssl端口的服务器中未定义“ssl\U证书”