服务器{ 监听 443 ssl; # 在端口 443 上侦听 HTTPS 流量 服务器名称 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 文件,我也检查了权限,一切都很好,但仍然无法正常工作
我手动尝试查看路径是否有静态文件,或者但所有静态文件都位于正确的位置
我正在使用下面的 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/;
}}