我想使用nginx只有一个位置用于根url( https://example.com/ ),并提供来自不同位置的所有其他文件。 最终我想用这个在代理之前进行身份validation,但最初我只是尝试简单的文件位置。
基于文档我使用两个位置块:
server { listen 443 ssl; ssl_certificate /etc/nginx/cert.pem; ssl_certificate_key /etc/nginx/cert.key; location = / { root /usr/share/nginx/html; index index.html; } location / { root /usr/share/nginx/html2; } }
但对https:// example.com/的请求正在获取/usr/share/nginx/html2/index.html而不是/usr/share/nginx/html/index.html
同样,我试过了:
location = / { root /usr/share/nginx/html; index index.html; } location ~* ^/.* { root /usr/share/nginx/html2; }
但是我得到了同样的结果。
有没有一个简单的方法来使这个工作?
这是因为索引指令将内部redirect到/index.html因此它与第二个位置块匹配。 将第一个块更改为:
location ~ ^/(index\.html)?$ { root /usr/share/nginx/html; index index.html; }