Nginx:我的 WordPress 博客目录中的 CSS 和 JS 文件提供错误

Nginx:我的 WordPress 博客目录中的 CSS 和 JS 文件提供错误

我为我的摄影网站设置了一个 Amazon ec2 LEMP 服务器,该网站以前使用 apache,我对它非常熟悉。

我一切运行正常,除了在博客目录中。CSS 和 JS 文件似乎由 PHP 提供,内容类型为 text/html,例如,以下是我的主题样式表的响应标头 ( /blog/wp-content/themes/twentyseventeen/style.css?ver=4.9.8):

content-type: text/html
date: Fri, 26 Oct 2018 02:33:26 GMT
server: nginx/1.12.2
status: 200
x-powered-by: PHP/5.4.16

与我自己的样式表的标题相比(/include/css/style.css):

accept-ranges: bytes
cache-control: max-age=315360000
content-length: 34199
content-type: text/css
date: Fri, 26 Oct 2018 02:48:04 GMT
etag: "5b7f653b-8597"
expires: Thu, 31 Dec 2037 23:55:55 GMT
last-modified: Fri, 24 Aug 2018 01:54:03 GMT
server: nginx/1.12.2
status: 200

我读过很多处理类似问题的帖子。但是,我感到很困惑,因为我的问题仅限于目录/blog/

我读过的其他一些问题/答案提到过security.limit_extensions,而且我的(/etc/php-fpm.d/www.conf)的设置如下:

security.limit_extensions =
;security.limit_extensions = .php .php3 .php4 .php5 .ttf

我改变了它:

;security.limit_extensions =
security.limit_extensions = .php .php3 .php4 .php5 .ttf

并通过 - 重新启动了 nginx service nginx restart,但问题仍然存在。

无法想象我错过了什么。准备放弃并切换回 Apache。:(

有人看到我错过了什么吗?

更新:配置文件

/etc/nginx/nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
#user ec2-user;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_names_hash_bucket_size 64;


    client_max_body_size 2M;

    include             mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/mikewillisphotography.com.conf

server {
    listen 80 default_server;
    server_name www.mikewillisphotography.com mikewillisphotography.com;
    return 301 https://www.mikewillisphotography.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name mikewillisphotography.com;
    return 301 https://www.mikewillisphotography.com$request_uri;
}

server {
    listen       443 ssl default_server;
    server_name  www.mikewillisphotography.com;
    #server_name localhost;

    include /etc/nginx/sites-available/includes/restrictions.conf;
    include /etc/nginx/sites-available/includes/wordpress.conf;
    #       include /etc/nginx/sites-available/includes/php.conf;

    ssl_certificate /etc/letsencrypt/live/mikewillisphotography.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mikewillisphotography.com/privkey.pem;

    location /.well-known/acme-challenge {
        #root /var/www/html/letsencrypt/wordpress/;
        root /usr/share/nginx/sites/mikewillisphotography.com/htdocs/letsencrypt/wordpress/;
    }

    client_max_body_size 2M;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    }

    location ~ \.php$ {
        include /etc/nginx/sites-available/includes/php.conf;
    }
}

/etc/nginx/sites-available/includes/php.conf

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

#wordpress stuff
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

/etc/nginx/sites-available/includes/wordpress.conf

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    include /etc/nginx/sites-available/includes/php.conf;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;
}

location @blog {
    rewrite ^/blog(.*) /blog/index.php?q=$1;
}

答案1

我发现问题了 - 我没有检查 wordpress.conf 文件,但确实它包含了目录下每个请求的 php.conf 文件/blog/

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    include /etc/nginx/sites-available/includes/php.conf;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;
}

我将其改为使用嵌套位置块来捕获 .php 文件,这解决了问题。不确定这是否是最有效的方法,但它确实有效。

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;

    location ~ \.php$ {
        include /etc/nginx/sites-available/includes/php.conf;
    }
}

相关内容