gpt4 book ai didi

php - Nginx 缓存 404

转载 作者:行者123 更新时间:2023-12-02 00:50:41 25 4
gpt4 key购买 nike

我将 WordPress 与 Nginx 一起使用,但每当我尝试启用静态文件缓存时,它们都会显示 404 not found。

这是我的/etc/nginx/conf.d/default.conf 文件:

server {
listen 80;
server_name _;

# SSL configuration
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/shivampaw.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shivampaw.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_dhparam /etc/nginx/ssl/dhparams.pem;
ssl_session_timeout 30m;
ssl_session_cache shared:SSL:10m;
ssl_buffer_size 8k;
add_header Strict-Transport-Security max-age=31536000;

location / {
root /home/shivam/sites/shivampaw.com;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /home/shivam/sites/shivampaw.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json applicationx-javascript text/xml application/xml application/xml+rss text/javascript;

include wordpress/wordpress.conf;

这是 WordPress.conf 文件:

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}


# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

有了它,它工作正常。但是如果我添加

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}

到 wordpress.conf 的末尾并重新启动 nginx 并重新加载我的站点,所有静态文件都会出现 404。

所以我必须评论缓存出来并且它工作正常。

有什么想法吗?

404s Single 404

这意味着如果我包含 wp-rocket.conf(我使用它)它也会给出 404,因为 wp-rocket.conf 中有缓存内容。

基本上,为什么我不能缓存静态文件?

最佳答案

这是因为您已经在根位置 block 中定义了根。所以其他位置 block 不知道它。这是不好的做法(参见 here )。这些位置 block 不知道去哪里寻找文件。

您应该在 location/{ block 上方有 root/home/shivam/sites/shivampaw.com; 然后添加 try_files $uri $ uri//index.php?$args 到每个静态 block 。

另一种方法是保留您的配置,但将 alias/path/to/static/files/; 添加到每个 block 。

关于php - Nginx 缓存 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40239560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com