【发布时间】:2013-10-20 16:30:23
【问题描述】:
我实际上有这个工作,但我想知道我是否以最有效的方式这样做,或者我是否可以对我的 conf 文件进行任何改进。这是我正在尝试做的事情:
- 如果从根目录请求任何文件,我们应该始终提供“index.html”。不应访问其他文件,并且请求其他任何文件都应视为您请求“index.html”。目前我正在使用重写,但重定向也可以,而且可能更可取。
- 可以请求“/css”或“/js”下的任何文件,从那些不存在的目录请求文件应返回 404。
这是我当前的工作配置文件:
server {
listen 80;
server_name www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
location = /index.html {
}
# map everything in base dir to one file
location ~ ^/[^/]*$ {
rewrite ^/[^/]*$ /index.html;
}
location ~ ^/css/ {
}
location ~ ^/js/ {
}
}
更新
我的最终 conf 文件在负载测试下速度更快,并且比原始文件更简单,在这里:
server {
listen 80;
server_name example.com;
root /var/www/register;
location = /index.html {
}
# Default location, request will fallback here if none other
# location block matches
location / {
rewrite ^.*$ /index.html redirect; # 'root' location '/'
}
location /css/ {
}
location /js/ {
}
}
【问题讨论】:
-
虽然您的解决方案在技术上是正确的,但我建议将除 index.html(和 /css/、/js/)之外的所有内容移出 /var/www/mysite。 Nginx 是一个网络服务器,它的工作就是向网络提供文件。其他一切都是 hack ;)
标签: nginx