alias 和 root 的区别
一般情况下,在location /中配置root,在location /xxx中配置alias是一个好习惯。
假如服务器路径为 /home/imooc/files/img/face.png
- root路径完全匹配访问
location /imooc {
root /home;
}
用户访问的时候请求为 url:port/imooc/files/img/face.png
- alias可以为你的路径做一个别名, 对用户透明
location /hello {
alias /home/imooc;
}
用户访问的时候请求为 url:port/hello/files/img/face.png , 如此相当于为目录/home/imooc取了一个别名
location /images {
alias /usr/share/nginx/html/images;
}
location /demo {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 与上面的配置实现效果相同
# location /demo {
# alias /usr/share/nginx/html/demo;
# try_files $uri $uri/ /index.html;
# index index.html index.htm;
# }
反向代理proxy_pass URL末尾加与不加/(斜线)的区别
举例说明,假设前端访问http://192.168.217.129/prod-api/captchaImage
nginx配置如下,proxy_pass URL后面没有/
经过nginx转发后实际到后台的请求地址是http://192.168.217.129/prod-api/captchaImage
nginx配置如下,proxy_pass URL后面有/
经过nginx转发后实际到后台的请求地址是http://192.168.217.129/captchaImage
区别就是加/(斜线)后的url不带location的匹配参数
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 81;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 82;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location / {
root ss;
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:30002/api/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 86;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location / {
root tt;
index index.html index.htm;
try_files $uri $uri/ /index.html;
client_max_body_size 200m;
}
location /api {
proxy_pass http://10.39.16.95:30002/api/;
}
location /login {
proxy_pass http://10.39.16.95:30002/login/;
}
location /updatePass {
proxy_pass http://10.39.16.95:30002/updatePass/;
}
location /workflows {
proxy_pass http://10.39.16.95:30020/workflows/;
}
location /core {
client_max_body_size 200m;
proxy_pass http://10.39.16.95:30010/core/;
}
location /yxStatic {
proxy_pass http://10.39.16.93:8052/;
}
location /bhtimage {
alias D:/picture/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 89;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location / {
root daping;
index index.html index.htm;
}
location /core {
client_max_body_size 200m;
proxy_pass http://10.39.16.95:30010/core/;
}
location /BHTWebService {
proxy_pass http://10.39.16.91:2197/BHTWebService/;
}
location /api/AlarmReport/ {
proxy_pass http://10.39.16.93:8051/api/AlarmReport/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 83;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location / {
root dd;
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:30002/api/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 85;
server_name 10.39.16.95;
#charset koi8-r;
# access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|pdf|jpeg|png)$
{
#root D:/nginx-1.15.5/temp/images/;#指定图片存放路径(可以放在nginx文件夹路径里也可以放其他p盘)
root D:/picture/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}