对于一个即将上线的网站,我们经常在我们的网站根目录中有一个静态的index.html 。 该文件包含所有样式和图像内联,所以没有其他请求加载该页面。
现在,我使用这个configuration:
# Temporary placeholder server { server_name .domain.com; root /var/www/domain/production/public; index index.html; charset UTF-8; } # Production server { server_name test.domain.com; root /var/www/domain/production/public; charset UTF-8; gzip_types text/plain application/xml text/css text/js image/svg+xml text/xml application/x-javascript text/javascript application/json application/xml+rss; location /assets/images { default_type image/jpeg; expires max; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } location /styles/fonts { expires max; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } include conf.d/common.conf.inc; }
这个工作非常“足够”,但是ppl能够请求不应该服务的资源(例如domain.com/foo/bar/file.ext),因为只有“index”指令被设置。
问题:如何将所有请求指向第一个服务器块中的index.html ? 我尝试使用try_files使用位置,但nginx无法处理该列表中的单个项目:
location / { try_files index.html; }
我怎样才能做到这一点? (我知道你可以使用redirect,但现在我不喜欢这个解决scheme。domain.com/foo/bar/baz应该只是为index.html服务)。
我想你需要这个:
location = /index.html { root /var/www/domain/production/public; } location / { rewrite . /index.html last; }
查看Nginx的位置文档,了解它的含义。