标签:nginx root alias
先看官方文档
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias http://nginx.org/en/docs/http/ngx_http_core_module.html#root
location /request_path/image/
{
root /local_path/image/;
}nginx中配置
root@leco:/etc/nginx/sites-enabled# ls
default
root@leco:/etc/nginx/sites-enabled# egrep -v '#|^$' default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location /request_path/image/ {
root /local_path/image/;
}
location / {
try_files $uri $uri/ =404;
}
}
root@leco:/etc/nginx/sites-enabled# /etc/init.d/nginx reload
[ ok ] Reloading nginx configuration (via systemctl): nginx.service.实际图片路径
root@leco:/local_path/image/request_path/image# pwd /local_path/image/request_path/image root@leco:/local_path/image/request_path/image# ls 1.png
这样配置的结果就是当客户端请求 /request_path/image/1.png 的时候,
Nginx把请求映射为/local_path/image/request_path/image/1.png
web访问效果

location /request_path/image/
{
alias /local_path/image/;
}这时候,当客户端请求 /request_path/image/2.png 的时候,
Nginx把请求映射为/local_path/image/2.png
nginx配置
root@leco:/local_path/image# ls
2.png request_path
root@leco:/local_path/image# pwd
/local_path/image
root@leco:/etc/nginx/sites-enabled# egrep -v '#|^$' default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location /request_path/image/ {
alias /local_path/image/;
}
location / {
try_files $uri $uri/ =404;
}
}
root@leco:/etc/nginx/sites-enabled# /etc/init.d/nginx reload
[ ok ] Reloading nginx configuration (via systemctl): nginx.service.图片位置
root@leco:/local_path/image# pwd /local_path/image root@leco:/local_path/image# ls 2.png request_path
web访问效果

标签:nginx root alias
原文地址:http://blog.51cto.com/caimengzhi/2091527