#### nginx根据域名后缀流入不同的项目
简介:我们的项目是前后端分离,前端使用vue写的
话不多说,贴配置文件文件
```
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 6500;
#gzip on;
fastcgi_intercept_errors on;
client_max_body_size 2m;
server {
listen 80;
server_name 你的域名;
#charset koi8-r;
#access_log logs/host.access.log main;
#域名后缀需要前端在自己项目配置,如https://www.my-app.com/my-app/ 的my-app
location /my-app {
try_files $uri $uri/ /index.html?$query_string;
alias 你的前端项目路径1;
index index.html index.htm;
}
#前端配置的代理,所有项目访问需要加上my-app-api
location /my-app-api/ {
rewrite ^/my-app-api/(.*)$ /$1 break;
proxy_pass http://ip:端口1;
}
location /my-app2 {
try_files $uri $uri/ /index.html?$query_string;
alias 你的前端项目路径2;
index index.html index.htm;
}
location /my-app-api2/ {
rewrite ^/my-app-api2/(.*)$ /$1 break;
proxy_pass http://ip:端口2;
}
location /my-app3 {
try_files $uri $uri/ /index.html?$query_string;
alias 你的前端项目路径3;
index index.html index.htm;
}
location /my-app-api3/ {
rewrite ^/prod-api/(.*)$ /$1 break;
proxy_pass http://ip:端口3;
}
error_page 404 /404.html;
location = /404.html {
root html;
}
location ~ /404.png$ {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location = /MP_verify_HUcHJRz2Vy6jnbNX.txt {
root html;
}
location = /MP_verify_tLTDZvfsfovdX16T.txt {
root html;
}
}
#第一个是根据不同的后缀流入不同的前端项目,下面这个是另外的域名和项目
server {
listen 80;
server_name 第二个域名;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 你的前端项目路径;
index index.html index.htm;
}
location /代理名字(前端项目配置) {
rewrite ^/代理名字/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8081;
}
error_page 404 /404.html;
location = /404.html {
root html;
}
location ~ /404.png$ {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location = /MP_verify_HUcHJRz2Vy6jnbNX.txt {
root html;
}
location = /MP_verify_tLTDZvfsfovdX16T.txt {
root html;
}
}
}
此处配置已经达到要求,如果可以,可以再优化一下
例如:
upstream xx_server{
server 127.0.0.1:8081;
}
代理中可以这样写
proxy_pass http://xx_server;
大同小异
```

nginx根据域名后缀流入不同的项目