直接上代码,配置nginx.conf文件:
upstream gateway {
server 192.168.2.1:8888;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /app/vue/html #代码路径
index index.html index.htm;
}
location ^~/api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://gateway/;
}
请求之前的地址:http://192.168.2.1:8888/wtdf/json/bb/user/login
请求之后的地址:http://192.168.2.1/api/wtdf/json/bb/user/login
location ^~/api/ {} 代表请求接口是要加api
一些概念:
1.全等匹配:
location = /static/img/ {
deny all;
}
2.普通匹配:
location /static/img/ {
deny all;
}
# 匹配到此类型时,终止后续的正则匹配(后面会有详细分析)
location ^~/static/img/ {
deny all;
}
3.正则匹配:
#正则不区分大小写
location ~*/static/img/ {
deny all;
}
#正则区分大小写
location ~/static/img/ {
deny all;
}