它的长短是,我已经为我的代码(/ main-site / sub)添加了一个子目录,我想将它添加到我的nginx配置中。但是,我无法阻止子域加载错误(some.web.site/sub/sub/index.html)而不是(some.web.site/sub/index.html)。
这是配置文件:
server {
listen 80;
server_name site.com www.site.com ~^((?<subdomains>.+)?\.)?site.com$;
root /srv/site;
index index.html index.htm;
access_log /var/log/nginx/site/access.log;
error_log /var/log/nginx/site/error.log;
if ($subdomains !~* "^(www)?$") {
rewrite ^/(.*)$ https://$subdomains.site.com/$1 redirect;
}
rewrite ^/(?!index)(.*).html$ $1 permanent;
rewrite ^/stuff1$ /st1 redirect;
rewrite ^/stuff2$ /st2 redirect;
rewrite ^/stuff3$ /st3 redirect;
rewrite ^/stuff4$ /st4 redirect;
rewrite ^/stuff5$ /st5 redirect;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to 404
# hide html extension
try_files $uri.html $uri/ =404;
}
## Where this stays in or not the result is the same
## site.com/sub/sub/index and not site.com/sub/index
## location /sub/ {
## root /srv/site;
## }
# Redirect 404 to index
error_page 404 = @fallback;
location @fallback {
rewrite .* / permanent;
}
}
谢谢!
答案 0 :(得分:0)
以下是解决方案:
rewrite ^/(?!index)(.*).html$ /$1 permanent;
我必须在$ 1之前添加/。