nginx部署多个vue项目

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;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        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 /admin/ {
            root /root/cms-admin/dist;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
        }
        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/admin返回500,请问是哪里出了问题
可以确定/root/cms-admin/dist下存在index.html文件

阅读 3.1k
4 个回答

粗看起来配得没问题呀。你确定xxx.com:3000/home这个页面存在吗?前端若是单页应用的话,就需要try_files到index.html

这样看不出来问题,你把配置全发出来看看。如果不方便的话,可以在 location 中通过 return 600; 方式,一步步调试,确认哪里出问题了。

理解错了try_files的用途,解决方案就是将try_files $uri $uri/ =404删了,因为前端的页面是在请求进来后根据路由在服务端把页面渲染完返回给前端的,因此找不到对应文件及文件夹直接重定向404了

新手上路,请多包涵
# 在dist文件夹里创建个admin文件夹应该就行了,把静态文件放入admin文件夹就行
location /admin {
            root /root/cms-admin/dist;
            try_files $uri $uri/ /admin/index.html;
            index index.html index.htm;
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题