如何使用 Nginx 使用简单的 php 应用程序配置 wordpress 应用程序?
我想用一个简单的 php 应用程序配置一个 Wordpress 应用程序.应用程序的目录结构如下:
I want to configure a Wordpress application with a simple php application. The directory structure of the application is as follow :
根目录:/var/www/demoApp/
Root directory : /var/www/demoApp/
Wordpress 目录:/var/www/demoApp/wordpress/
Wordpress directory : /var/www/demoApp/wordpress/
在这里,我想使用路由 http://BASE_URL/wordpress 访问 wordpress 应用程序.但我无法配置 Web 服务器./var/www/demoApp/目录下的所有 php 页面都可以使用 url http://BASE_URL/ 正常工作.虽然 wordpress 文件没有被正确加载.
Here i want to access the wordpress application using route http://BASE_URL/wordpress. But i am not able to configure the web server. All the php pages under /var/www/demoApp/ directory are working fine using url http://BASE_URL/. While wordpress files are not being loaded correctly.
这是我的 Nginx 配置块:
Here is my Nginx configuration block :
server
{
listen 80;
root /var/www/demoApp;
index index.php index.html index.htm;
server_name localhost;
error_page 500 404 /404.php;
location /
{
try_files $uri $uri/ /index.php?$query_string;
}
location /wordpress
{
try_files $uri $uri/ /index.php?$query_string;
rewrite ^(.*)$ /wordpress/index.php?$1 last;
location ~ \.php
{
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ~ \.php$
{
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
配置可能有什么问题?
您为两个应用程序使用了一个共同的 root
,因此嵌套的 location ~ \.php
块是不必要的(并且在您的配置中从未被采用).有关更多信息,请参阅本文档.
You are using a common root
for both applications, therefore the nested location ~ \.php
block is unnecessary (and in your configuration is never taken). See this document for more.
try_files
和 rewrite
是冲突的,一个 try_files
语句就足够了.有关详细信息,请参阅本文档.
The try_files
and rewrite
are conflicting, and a single try_files
statement is adequate. See this document for details.
你应该尝试这样的事情:
You should try something like this:
server {
listen 80;
root /var/www/demoApp;
index index.php index.html index.htm;
server_name localhost;
error_page 500 404 /404.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /wordpress {
try_files $uri $uri/ /wordpress/index.php;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}