Redirect 将除一个位置外的所有通信重定向到SSL
我有一台运行的nginx服务器为我的网站服务。出于安全原因,所有连接都重定向到SSL 然而,我正在拼命寻找如何从这个重定向中排除一个位置。我已经试过重写、重定向、代理传递等,但似乎不起作用 我不想(301或302)重定向我的站点,我只希望SSL是可选的。不同类型文件(js、php、html)的位置 比如说Redirect 将除一个位置外的所有通信重定向到SSL,redirect,ssl,nginx,Redirect,Ssl,Nginx,我有一台运行的nginx服务器为我的网站服务。出于安全原因,所有连接都重定向到SSL 然而,我正在拼命寻找如何从这个重定向中排除一个位置。我已经试过重写、重定向、代理传递等,但似乎不起作用 我不想(301或302)重定向我的站点,我只希望SSL是可选的。不同类型文件(js、php、html)的位置 比如说 server { listen 80; server_name example.com root /var/www/example; location /un
server {
listen 80;
server_name example.com
root /var/www/example;
location /unsafe {
try_files $uri $uri/ /index.php;
}
location / {
rewrite ^ https://$server_name$request_uri? permanent;
}
# other rules...
}
server {
listen 443;
server_name example.com
root /var/www/example;
location / {
try_files $uri $uri/ /index.php;
}
# other rules...
}
不起作用
我还尝试使用重定向
或重写
,而不是尝试文件
,但一点运气都没有。问题是,我不希望流量被重定向、重写或代理,我只希望nginx在example.com/unsafe上传递所有请求
我得到的只是一堆404和502
我做错了什么
Cheers对于正常http连接(在端口80上)和https SSL连接(在端口443上),您应该有单独的服务器块 修订守则: 如果您希望站点上的所有文件都使用https连接(SSL,端口443),但/unsafe目录中的文件除外,则服务器块应该是这样的:
# This server block handles all requests on port 80 and serves only files inside
# the /unsafe directory. Everything else is redirected to an SSL connection on
# port 443.
server {
listen 80;
server_name your-domain.com
root /var/www/;
# only serve requests to files in the /unsafe directory
location /unsafe {
try_files $uri $uri/ =404;
}
# all other locations redirect to https connection
location / {
return 301 https://your-domain.com$request_uri;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ /unsafe/.*\.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}
# This server block handles all SSL (port 443) connections.
server {
listen 443;
server_name your-domain.com
root /var/www/;
location / {
try_files $uri $uri/ =404;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ \.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}
啊,对不起,我不太清楚。我已经这样做了,但是错误在于端口80的
位置设置,我想我不确定我是否理解您的问题。如果您希望SSL连接是可选的,那么您的服务器块可以同时侦听90和443,并且位置规则对这两个都有效。但是,如果您希望某个目录下的文件(对于exmaple,“/unsafe”)由没有SSL的正常http连接提供服务,那么在侦听端口80的服务器块下,您可以有一个与目录匹配的位置规则,并且您必须重定向所有其他连接以使用https(端口443).我更新了我的初始问题,以更好地突出我试图做的事情!您的问题仍然有点不清楚:您说,“出于安全原因,所有连接都被重定向到SSL”,但您也说,“我不想(301或302)重定向我的站点,我只希望SSL是可选的”——这两者是矛盾的。我已经添加了服务器块示例来处理我认为您正在尝试做的事情,但我仍然不确定您的目标到底是什么。好的,谢谢。我需要等到凌晨才能应用设置,否则服务器上的流量太大。我会让你不断更新!
# This server block handles all requests on port 80 and serves only files inside
# the /unsafe directory. Everything else is redirected to an SSL connection on
# port 443.
server {
listen 80;
server_name your-domain.com
root /var/www/;
# only serve requests to files in the /unsafe directory
location /unsafe {
try_files $uri $uri/ =404;
}
# all other locations redirect to https connection
location / {
return 301 https://your-domain.com$request_uri;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ /unsafe/.*\.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}
# This server block handles all SSL (port 443) connections.
server {
listen 443;
server_name your-domain.com
root /var/www/;
location / {
try_files $uri $uri/ =404;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ \.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}