我有以下域名 fairgrounds.mohave.gov。如果用户只输入 fairgrounds.mohave.gov,我希望它转到以下路径:
https://fairgrounds.mohave.gov/parks/fairgrounds/
否则,就正常操作。以下是配置:
server {
listen 80;
# listen 443;
server_name fairgrounds.mohave.gov;
# return 301 https://$host$request_uri;
# return 301 https://fairgrounds.mohave.gov/parks/fairgrounds/;
return 301 https://$host/$request_uri;
}
server {
listen 443 ssl http2;
server_name fairgrounds.mohave.gov;
root /var/www/parks2;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must_revalidate, proxy-revalidate, max-age=0';
if_modified_since off;
expires off;
etag off;
}
}
我尝试了几种方法来显示上述示例中注释掉的部分,但似乎不起作用。有什么建议吗?
答案1
您需要在第二个块中/
进行外部重定向。/parks/fairgrounds/
server
尝试:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
...
location = / {
return 301 /parks/fairgrounds/;
}
location / {
...
}
}
第一个location
块仅匹配/
并将访问者从外部重定向到https://example.com/parks/fairgrounds/
。第二个location
块是您现有的位置块,并处理其他所有事情。请参阅这个文件了解详情。