服务代理配置/api与/api/ 的区别
访问路径都为http://127.0.0.1:82/api/info
location /api/ {
proxy_pass http://127.0.0.1:82;
}
# 代理后的路径为http://127.0.0.1:82/api/info
location /api/ {
proxy_pass http://127.0.0.1:82/;
}
# 代理后的路径为http://127.0.0.1:82/info
proxy_pass http://127.0.0.1:82 没有后缀/ 会拼接代理前缀
proxy_pass http://127.0.0.1:82/ 有后缀/ 会舍弃代理前缀
独立的两个应用,同一个端口
- nginx的安装路径为: /opt/nginx
- 项目都在html下
- 项目A名称为: project1
- 项目B名称为: project2
- 监听端口都为82,在同一个server下
页面代理配置
location /api {
root html/project1;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 可以访问路径为: http://127.0.0.1:82/api
location /apa {
alias html/project2;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 可以访问路径为: http://127.0.0.1:82/apa
一个server模块下,不能有两个location 中 都为root 配置文件夹
但是可以有多个alias,alias 与 root功能相同,都是规定文件夹指向
这里用了一个root 一个 alias
独立的两个应用,不同端口
- nginx的安装路径为: /opt/nginx
- 项目都在html下
- 项目A名称为: project1
- 项目B名称为: project2
- 项目A监听端口都为82
- 项目A监听端口都为83
- 用两个server模块配置
页面代理配置
server {
listen 82;
server_name localhost;
location / {
root html/project1;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
server {
listen 83;
server_name localhost;
location / {
root html/project2;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
由于是不同的server ,所以用root 或者 alias 没有任何区别
子文件夹的两个应用,同一个端口
- nginx的安装路径为: /opt/nginx
- 项目A名称为: project1
- 项目B名称为: project2
- 监听端口为82
- 项目A路径为html
- 项目B路径为html/test1
页面代理配置1
server {
listen 82;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
root html/test1;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
页面代理配置2
server {
listen 82;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:89/;
}
}
server {
listen 89;
server_name localhost;
location / {
root html/test1;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}