nginx 命令
重启 nginx
nginx -s reload
nginx 配置
配置资源查找路径
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
location /i/ {
root /data/w3/images/;
}
# 请求 /i/test.jpg , 查找资源的路径为 /data/w3/images/i/test.jpg
location /i/ {
alias /data/w3/images/;
}
# 请求 /i/test.jpg , 查找资源的路径为 /data/w3/images/test.jpg
root
和 alias
字段的区别是alias
配置的是路径别名,不会带上匹配的前缀。且二者的使用范围不同。root
可以使用在 http、server、location 中,但 alias
只能使用在 location 中。
配置首页
http://nginx.org/en/docs/http/ngx_http_index_module.html
location / {
index index.html index.htm;
}
配置文件查找顺序
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
当匹配一个 location 时,可以设置匹配到的资源的查找顺序,从而可以设置一个查找不到时的默认返回值。
location / {
root /data/w3/;
try_files $uri $uri/ /index.html;
}
# 请求 /test 的时候先找 /data/w3/test 资源,再找 /data/w3/test/ 文件夹。都找不到时,
# 就重新请求 /index.html。
location /i/ {
root /data/w3/;
try_files $uri $uri/ /i/index.html;
}
# 请求 /i/test 的时候先找 /data/w3/i/test 资源,再找 /data/w3/i/test/ 文件夹。都找不到时,
# 就重新请求 /i/index.html。
try_files 里的 $uri 变量就是 location 匹配的路径值。
这种配置常常用于 history
路由模式。
HTML5 History 模式 | Vue Router
配置代理
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
location /test-proxy {
proxy_pass http://xxx.xx.x
}
# 访问 /test-proxy 路径,都会被转发到 proxy_pass 指定的服务器