Nginx不使用alias和root服务静态文件?

ru9i0ody  于 2023-11-17  发布在  Nginx
关注(0)|答案(2)|浏览(133)

我一直在尝试使用NGINX服务器提供一些静态文件,但尽管配置,我还没有能够让它运行。我已经尝试使用别名,根,甚至正则表达式匹配-什么可能会丢失?
/home/user/coka/staticfiles/包含了所有我想提供的文件,但每当我访问http://127.0.0.1/staticfiles/file.csshttp://example.com/staticfiles/file.css时,它都不显示。
我的配置如下所示:

server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name         example.com;
   
    
    location /staticfiles/ {

        root /home/user/coka/staticfiles/;

        access_log /home/user/coka/logs/nginx-static-access.log;
        error_log /home/user/coka/logs/nginx-static-error.log;
    }
}

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    return      301 https://example.com$request_uri;
}

字符串
404错误提示

bvuwiixz

bvuwiixz1#

你能试试以下方法吗?

ssl_session_cache shared:SSL:4m; # measured in megabytes, not minutes
        ssl_buffer_size 4k; # reduced from the default 16k to minimize TTFB
        ssl_session_timeout 60m;
        ssl_session_tickets off;
        ssl_dhparam /etc/ssl/nginx/dhparam.pem; # create with "openssl dhparam -out dhparam.pem 4096"
        ssl_ecdh_curve X25519:sect571r1:secp521r1:secp384r1;
        ssl_prefer_server_ciphers off;
        ssl_protocols TLSv1.3 TLSv1.2;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; 

        ssl_certificate /etc/ssl/chain.pem;
        ssl_certificate_key /etc/ssl/key.pem; 

server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name         example.com;
   
    root /home/user/coka;
    access_log /home/user/coka/logs/nginx-static-access.log;
    error_log /home/user/coka/logs/nginx-static-error.log;
}

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    return      301 https://example.com$request_uri;
}

字符串

11dmarpk

11dmarpk2#

它可能是由html文件中的src URL引起的。
使用带有“./"的相对路径:

<link rel="stylesheet" href="./style.css">

字符串
而不是静态的“/":

<link rel="stylesheet" href="/style.css">


如果你使用的是像Vite或Webpack这样的构建工具,它通常可以在配置文件中设置类似“base”的选项。

相关问题