Sri*_*aju 36
为什么这样的东西有用?乍一看,我不确定是否可以完成.但它提出了一个有趣的问题.
您可以尝试在配置文件中放置重定向语句并重新启动服务器.有两种可能性:
如果我想出更具体的东西,会增加更多.
更新:( 几小时后) 您可以试试这个.你需要把它放在你的nginx.conf文件中 -
server {
listen 443;
server_name _ *;
rewrite ^(.*) http://$host$1 permanent;
}
Run Code Online (Sandbox Code Playgroud)
向客户端发送永久重定向.我假设你使用端口443(默认)为https.
server {
listen 80;
server_name _ *;
...
}
Run Code Online (Sandbox Code Playgroud)
添加此项以便端口80上的正常http请求不受干扰.
更新: 2016
年12月18日 - server_name _应该使用而不是server_name _ *nginx版本> 0.6.25(感谢@Luca Steeb)
Zen*_*xer 35
rewrite并且if应该避免使用Nginx.着名的一句是"Nginx不是Apache":换句话说,Nginx有更好的方法来处理URL而不是重写. return从技术上讲,它仍然是重写模块的一部分,但它并没有带来开销rewrite,而且并不像它那样需要注意if.
Nginx已经对整个页面,为什么if是"邪恶".它还提供了一个建设性的页面,解释了为什么rewrite和if不好,以及如何解决它.下面是页面有说关于rewrite和if:
这是一种错误,繁琐且无效的方式.
您可以妥善解决这个问题,使用return:
server {
listen 443 ssl;
# You will need a wildcard certificate if you want to specify multiple
# hostnames here.
server_name domain.example www.domain.example;
# If you have a certificate that is shared among several servers, you
# can move these outside the `server` block.
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/cert.key;
# 301 indicates a permanent redirect. If your redirect is
# temporary, you can change it to 302 or omit the number
# altogether.
# $http_host is the hostname and, if applicable, port--unlike $host,
# which will break on non-standard ports
# $request_uri is the raw URI requested by the client, including any
# querystring
return 301 http://$http_host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
如果你期望很多机器人不发送Host标题,你可以使用$host而不是$http_host只要你坚持端口80和443.否则,你需要动态填充$http_host替代品.尽管使用了代码,但只要它出现在server(而不是location块中)的根中,该代码就是高效且安全的if.但是,您需要使用默认服务器才能应用此功能,应使用https避免使用.
set $request_host $server_name:$server_port;
if ($http_host) {
set $request_host $http_host;
}
Run Code Online (Sandbox Code Playgroud)
如果要为特定路径强制执行SSL/TLS,则禁止其他方式:
server {
listen 443 ssl;
server_name domain.example;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/cert.key;
location / {
return 301 http://$host$request_uri;
}
location /secure/ {
try_files $uri =404;
}
}
server {
listen 80;
server_name domain.example;
location / {
try_files $uri =404;
}
location /secure/ {
return 301 https://$http_host$request_uri;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的服务器没有与客户端直接通信 - 例如,如果您正在使用CloudFlare - 事情会变得复杂一些.您需要确保与客户端直接通信的任何服务器X-Forwarded-Proto都为请求添加了适当的标头.
使用这个是一个混乱的主张; 有关完整说明,请参阅IfIsEvil.为了使其有用,由于各种复杂的原因,if块不能在location块内.这会强制使用rewriteURI测试. 简而言之,如果你必须在生产服务器上使用它...不要.可以这样想:如果你已经超越了Apache,那么你已经超越了这个解决方案.
/ secure,/ secure /,/ secure /中的任何内容都将强制执行https,而所有其他URI将强制执行http.该(?! )PCRE构造是式断言. (?: )是一个非捕获组.
server {
# If you're using https between servers, you'll need to modify the listen
# block and ensure that proper ssl_* statements are either present or
# inherited.
listen 80;
server_name domain.example;
if ($http_x_forwarded_proto = https) {
rewrite ^(?!/secure)/ http://$http_host$request_uri? permanent;
}
if ($http_x_forwarded_proto != https) {
rewrite ^/secure(?:/|$) https://$http_host$request_uri? permanent;
}
}
Run Code Online (Sandbox Code Playgroud)
location / {
if ($scheme = https) {
rewrite ^(.*)? http://$http_host$1 permanent;
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题更适合 serverfault.com 站点。
重定向到 http 的更好方法:
server {
listen 443;
return 301 http://$host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
这避免了重写中的“if”子句和正则表达式,这些都是迄今为止其他解决方案的特征。两者都有性能影响,但在实践中你必须有相当多的流量才重要。
根据您的设置,您可能还想在 listen 子句中指定一个 ip,也许还需要在上面的 servername 子句中指定。照原样,它将适用于所有域名的所有端口 443 请求。您通常希望每个域都有一个带有 https 的 IP,因此将上述内容绑定到 IP 比将其绑定到域名更重要,但也有一些变化,例如所有域都是一个域的子域。
编辑:TLS 现在接近于通用,并且有了它的服务器名称标识 (SNI),它允许多个域上的 HTTPS 站点共享一个 IP。有一个很好的写了这里
| 归档时间: |
|
| 查看次数: |
78057 次 |
| 最近记录: |