我一直在将Apache用于一个项目,并且由于项目已经增长很多,现在已经决定将性能转换为nginx。
对于这个项目,我们通过HTTPS服务我们的基本域和www子域,但需要通过HTTP服务所有其他子域。
在Apache中,我能够通过RewriteEngine完成此操作:
RewriteEngine On
#Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [OR]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我的网站有关于nginx的一半设置,这部分配置让我很难过。如何将其转换为使用nginx?
答案 0 :(得分:1)
我将答案分成4个虚拟主机。前两个解决了http到主域之间的https重定向问题。第二部分捕获子域并从https重定向到http:
# FIRST PART ---------------
# from http to https on main domains
server {
listen 80;
server_name domain www.domain;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name domain www.domain;
# blah, blah, https and virtualhost configuration
}
# SECOND PART ---------------
# from https to http and others subdomains
server {
listen 443 ssl;
server_name *.domain;
# blah, blah, https configuration
location / {
return 301 http://$host$request_uri
}
}
server {
listen 80;
server *.domain;
# virtual with http configuration
}