nginx 重定向到索引页面
nginx redirection to index page
我正在努力实现从非索引页面到我的索引页面的自动 nginx 重定向,/admin 除外
例如,example.com/test 应该重定向到 example.com,但是 example.com/admin 不应该重定向到 example.com
这是我当前的 nginx 配置文件:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
我已经尝试在我的 location /
块中添加一个 try_files
语句,以及其他东西,但是 none 似乎有效。我错过了什么吗?
您正在尝试将 proxy_pass
与 try_files
混合使用,它不会在同一个 location
块中工作。您可以改用命名位置,并使用否定正则表达式断言将任何不以 /admin
开头的 URI 重写为根 URI:
location / {
try_files $uri @app;
}
location @app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
您不需要单独的 location /media { ... }
或 location /static { ... }
块,因为作为 nginx 文档 states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root
directive instead:
location /images/ {
root /data/w3;
}
相反,您只需要定义公共服务器根目录(在任何 location
块之外):
root /var/www/example.com;
您也不需要使用 proxy_ssl_server_name
指令,因为您没有使用 HTTPS 协议将您的请求代理到上游。
我正在努力实现从非索引页面到我的索引页面的自动 nginx 重定向,/admin 除外
例如,example.com/test 应该重定向到 example.com,但是 example.com/admin 不应该重定向到 example.com
这是我当前的 nginx 配置文件:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
我已经尝试在我的 location /
块中添加一个 try_files
语句,以及其他东西,但是 none 似乎有效。我错过了什么吗?
您正在尝试将 proxy_pass
与 try_files
混合使用,它不会在同一个 location
块中工作。您可以改用命名位置,并使用否定正则表达式断言将任何不以 /admin
开头的 URI 重写为根 URI:
location / {
try_files $uri @app;
}
location @app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
您不需要单独的 location /media { ... }
或 location /static { ... }
块,因为作为 nginx 文档 states:
When location matches the last part of the directive’s value:
location /images/ { alias /data/w3/images/; }
it is better to use the
root
directive instead:location /images/ { root /data/w3; }
相反,您只需要定义公共服务器根目录(在任何 location
块之外):
root /var/www/example.com;
您也不需要使用 proxy_ssl_server_name
指令,因为您没有使用 HTTPS 协议将您的请求代理到上游。