如何使用nginx和vite实现资产路径

d5vmydt9  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(129)

我使用nginx和vite来docker vue.js。但是,当我设置基本路径时,浏览器屏幕变为空白。返回/test/index html,但/test/assets/something. js和css也返回html。我如何重写vite + base + nginx,以便正确显示以下URL?
http://localhost:808/test/index
vite.confign.ts

base: '/test/',

default.conf

server {
    listen   8080 default_server;
    server_name  "";

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

    location ~ ^/assets/(.*)$ {
        proxy_set_header X-Forwarded-For $remote_addr;
        root /home/vue_server/assets/$1;
        try_files $uri $uri/ /index.html;
    }

    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        root /home/vue_server/;
        try_files $uri $uri/ /index.html;
    }
}
mnowg1ta

mnowg1ta1#

试试这个:

server {
    listen 8080 default_server;
    server_name _;

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

    # Include MIME types file if not defined previously
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    location /test/ {
        alias /home/vue_server/;
        try_files $uri $uri/ /test/index.html;
    }

    location / {
        return 301 /test/$uri;
    }
}

相关问题