2017-03-06 35 views
1

我已經安裝了一個WP博客,並在nginx服務器塊配置上爲它添加了一個位置塊。當訪問博客example.com/blog或任何博客文章如example.com/blog/foo-bar-baz時,將提供內容但缺少所有資產。PHP資產沒有被nginx服務

當我檢查日誌時,它看起來像對這些資產URL的所有請求都將永久重定向到根博客URL。下面是nginx的的訪問日誌的請求的例子:

"GET /blog/wp-includes/js/jquery.min.js HTTP/1.1" 301 5 "https://example.com/blog/" "USER_AGENT_STRING" 

下面是nginx的配置:

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

server { 
    listen 443 ssl; 
    server_name example.com www.example.com; 
    root /home/example/current/public; 
    index index.html index.htm; 

    location ~ /blog { 
    index index.php index.html index.htm; 
    fastcgi_param SCRIPT_FILENAME /var/www/blog/index.php; 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
    } 

    location ~ \.php$ { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    fastcgi_buffers 256 128k; 
    fastcgi_connect_timeout 300s; 
    fastcgi_send_timeout 300s; 
    fastcgi_read_timeout 300s; 
    include fastcgi_params; 
    } 
} 

如何編輯任一位置塊有nginx的捉對所有的URL請求是包含/blog/wp-content//blog/wp-includes/?我嘗試添加以下位置塊,但它不起作用:

location ~ ^(/wp-content/|/wp-includes/)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3)$ { 
    root /var/www/blog; 
} 

回答

1

您的配置似乎無法提供靜態內容。更傳統的方法是使用嵌套位置來提供動態內容。例如:

location ^~ /blog { 
    root /var/www; 
    index index.php index.html index.htm; 

    try_files $uri $uri/ /blog/index.php; 

    location ~ \.php$ { 
     try_files $uri =404; 

     include fastcgi_params; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_param SCRIPT_FILENAME $request_filename; 
    } 
} 

請參閱this document瞭解更多信息。