![在为 rails 配置的 nginx 服务器中以目录位置运行 wordpress 博客](https://linux22.com/image/714625/%E5%9C%A8%E4%B8%BA%20rails%20%E9%85%8D%E7%BD%AE%E7%9A%84%20nginx%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%AD%E4%BB%A5%E7%9B%AE%E5%BD%95%E4%BD%8D%E7%BD%AE%E8%BF%90%E8%A1%8C%20wordpress%20%E5%8D%9A%E5%AE%A2.png)
我一直在 nginx 下设置的 puma / rails 上运行一个网站。现在我们必须在路径 /blog 下设置 wordpress,即
www.mysite.com
是 rails 应用程序,www.mysite.com/blog
是 wordpress 博客。
Wordpress 博客的位置设置如下
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///var/run/website/website.sock fail_timeout=0;
}
server {
listen 80;
server_name www.bhokaal.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
#server_name www.bhokaal.com;
access_log /home/bhokaal/bhokaal.com/logs/access.log;
error_log /home/bhokaal/bhokaal.com/logs/error.log;
ssl_certificate /etc/ssl/www.bhokaal.com_cert_chain.crt;
ssl_certificate_key /etc/ssl/www.bhokaal.com.key;
root /home/bhokaal/bhokaal-website/public/;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ^~ /assets/ {
gzip_static on;
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
}
location /blog {
root /home/bhokaal/bhokaal-blog/;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 500m;
keepalive_timeout 15;
}
我的假设是 /blog 由 PHP 处理,而其余部分则转到上游 puma 服务器。
当我们访问时,我们得到了一个相当神秘的错误www.bhokaal.com/blog
2017/12/08 12:10:02 [error] 19193#19193: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 49.207.56.120, server: , request: "GET /blog/ HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.1-fpm.sock:", host: "www.bhokaal.com"
Nginx、PHP-FPM 和 Puma 都在同一个用户下运行,因此我不认为这是用户权限问题,正如我在有关此问题所引用的大多数博客所指出的那样。
答案1
您需要使用alias
将以 开头的 URI 映射/blog
到以其他内容结尾的根。请参阅这个文件了解更多信息。
最干净的解决方案是使用嵌套location
块。^~
外部块上的修饰符可避免任何副作用。请参阅这个文件了解更多信息。
location ^~ /blog {
alias /home/bhokaal/bhokaal-blog;
index index.php;
if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
答案2
这是对我有用的方法,
/home/bhokaal/bhokaal-website/public/blog
在ie 的 rails 公共文件夹下安装 wordpress
像往常一样添加位置和 PHP 块
location /blog {
index index.html index.htm index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}