nginx 在nginix中未找到静态文件错误,在正确的路径后mentiondd

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

server { listen 443 ssl;#在端口443上监听HTTPS流量server_name 54.198.4.208; #替换为您的实际域名或IP地址

# SSL/TLS Certificate Configuration
ssl_certificate /etc/nginx/selfsigned.crt; # Path to self-signed SSL certificate
ssl_certificate_key /etc/nginx/selfsigned.key; # Path to self-signed SSL certificate key
# Location Blocks for Handling Requests
location = /favicon.ico {
    access_log off;
    log_not_found off;
}

location /static/ {
    alias /home/ubuntu/logins/staticfiles/; # Path to the root directory of your Django project
}

location / {
    proxy_pass https://54.198.4.208:8000; # Replace with your Django app's IP and port
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

}这是我的nginix配置,当我尝试http://54.198.4.208:8000/static/style.css这个网址我得到了文件找不到404错误所有我的静态文件都存在于正确的目录,即使我试图打开一个静态文件手动通过这个路径,我得到了staitc文件在正确的路径也我检查了权限也一切都很好,但仍然无法工作
我试图手动查看路径是否有静态文件或但它在正确的位置有所有静态文件

z31licg0

z31licg01#

我使用下面的nginx配置,一切都是正确的。试试这个代码。

server {
listen 80;
server_name media.com;

root /path/to/static/directory/;
error_log /path/to/static/directory/;

gzip on;
gzip_comp_level    5;
gzip_min_length    256;
gzip_proxied       any;
gzip_vary          on;

gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;

location /static/ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
    alias /path/to/static/directory/;
}

location /media/ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
    alias /path/to/static/directory/;
}}

相关问题