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;
大同小异
变量解释
try_files 固定语法
$uri 指代home文件(ip地址后面的路径,假如是127.0.0.1/index/a.png,那就指代index/a.png)
$uri/ 指代home文件夹
/index.html 向ip/index.html 地址发起请求
try_files $uri $uri/ /index.html;
尝试解析下列2个文件/文件夹(自动分辨出,IP后面的路径是文件还是文件夹), $uri/$uri/,
如果解析到,返回第一个,
如果都没有解析到,向127.0.0.1/index.html发起请求跳转(该路由必须真实,不然会报错)
$arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值。
$args #这个变量等于请求行中(GET请求)的参数,例如?url=;
$binary_remote_addr #二进制的客户地址。
$body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$cookie_COOKIE #cookie COOKIE变量的值
$document_root #当前请求在root指令中指定的值。
$document_uri #与 $uri相同。
$host #请求主机头字段,否则为服务器名称。
$hostname #设置为gethostname返回的计算机主机名
$http_HEADER
$is_args #如果有 $args参数,这个变量等于”?”,否则等于”",空值。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$query_string #与 $args相同。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
$request_method #GET或POST
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。