目标: 同一域名, 同一端口, 实现 两个前端项目的无缝跳转和链接
1. 通过 location 指向需要跳转的目录
location /new-home/ {
alias /mnt/new/pgv/;
index index.html index.htm index.php;
}
注意:alias 和 root 的区别: root 跳转后会在路径后面添加匹配的路径 例如 location /aa/ { root /aa/bb/}
跳转后的结果为----> /aa/bb/aa/ 而 alias 则不会追加路径;
2. 下面这一步是解决 js、css、img等文件的404问题
我采用的方法是 rewrite 实现url的跳转解决的 路径404 的问题
rewrite ^/new-home/js/(.*)$ https://www.aa.com/js/$1 permanent;
3. 完美解决了!!!
话不多说,贴上完整的Nginx配置
server{
listen 443;
ssl on;
server_name www.aaa.com;
index index.html index.htm index.php;
root /mnt/test/;
location /new/ {
alias /mnt/test/new/;
index index.html index.htm index.php;
}
rewrite ^/new/static/js/(.*)$ https://www.aaa.com/static/js/$1 permanent;
rewrite ^/new/static/css/(.*)$ https://www.aaa.com/static/css/$1 permanent;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location /java-api/ {
keepalive_timeout 300s;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ip:8083/;
}
}