nginx在同一个域名下部署多个项目

nginx配置如下
nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        
        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

sites-enabled > default


server {
        listen 80 default_server;
        listen [::]:80 default_server;
        
        root /var/html
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;
        
        location /admin/ {
            alias /home/cms-admin/dist;
            index index.html index.htm;
        }
        
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://127.0.0.1:3000;
        }
        
        location ~ .*\.(js|css|png|jpg|gif|swf|ico|pdf|mov|mp3|wav|json|woff2)$ {
            proxy_pass http://127.0.0.1:3000;
        }

        location /api/ {
            proxy_pass  http://127.0.0.1:3002/api/;
        }

}

3000端口跑了一个ssr项目,现在的配置可以正常访问,例如:xxx/index,可以直接拿到ssr项目index页面

location ~ .*\.,这个规则是配合ssr项目的,去拿ssr项目的静态文件

/home/cms-admin/dist这个目录下是另一个项目的打包文件,我需要通过访问xxx/admin拿到dist目录下的index.html文件(如果把其他的规则都删除,只保留location /admin/这个规则,可以正常拿到页面)

现在的问题是:
当我访问xxx/admin的时候,直接进入location /这个规则中,返回了ssr项目中admin路由页面(ssr项目不存在这个路由,所以返回了一个空的页面)

而我希望的效果是访问xxx/admin路由,直接去拿dist目录下的index.html

阅读 4.2k
1 个回答
location ^~ /admin/ {
    alias /home/cms-admin/dist;
    index index.html index.htm;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题