在与Rails应用程序相同的域上运行Wordpress的最佳方法是什么?
我有一个标准的Rails应用程序,其中Nginx和Mongrel在http:// mydomain上运行。我需要在http://mydomain.com/blog上运行Wordpress博客。我的首选是将博客托管在运行在同一服务器或者单独的服务器上的Apache中,但我不希望用户在URL中看到其他服务器。那有可能吗?如果没有,我们会建议如何实现这一目标?
解决方案
在我看来,诸如重写操纵器之类的东西可以满足需求。对不起,我没有更多细节了,只是大声思考:)
实际上,由于我们使用的是Nginx,因此我们已经处于良好状态,不需要Apache。
我们可以通过fastcgi运行PHP(在Nginx Wiki中有关于如何执行此操作的示例),并在Nginx配置中使用URL匹配模式将某些URL定向到Rails,将其他URL定向到PHP。
这是一个用于通过PHP fastcgi运行WordPress博客的示例Nginx配置(请注意,我还输入了与WordPress .htaccess等效的Nginx,因此我们还将拥有已经使用该配置的精美URL):
server { listen example.com:80; server_name example.com; charset utf-8; error_log /www/example.com/log/error.log; access_log /www/example.com/log/access.log main; root /www/example.com/htdocs; include /www/etc/nginx/fastcgi.conf; fastcgi_index index.php; # Send *.php to PHP FastCGI on :9001 location ~ \.php$ { fastcgi_pass 127.0.0.1:9001; } # You could put another "location" section here to match some URLs and send # them to Rails. Or do it the opposite way and have "/blog/*" go to PHP # first and then everything else go to Rails. Whatever regexes you feel like # putting into "location" sections! location / { index index.html index.php; # URLs that don't exist go to WordPress /index.php PHP FastCGI if (!-e $request_filename) { rewrite ^.* /index.php break; fastcgi_pass 127.0.0.1:9001; } } }
这是我在上面的配置中包含的fastcgi.conf文件(我将其放在单独的文件中,因此我所有的虚拟主机配置文件都可以在正确的位置包含它,但我们不必这样做):
# joelhardi fastcgi.conf, see http://wiki.codemongers.com/NginxFcgiExample for source fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect #fastcgi_param REDIRECT_STATUS 200;
我也碰巧按照Nginx Wiki的建议进行操作,并使用Lighttpd的spawn-fcgi作为我的CGI-spawner(Lighttpd是一种非常快速的编译器,没有奇怪的依赖项,因此安装起来既快捷又容易),但是我们也可以为此,请使用简短的Shell / Perl脚本。
我认为joelhardi的解决方案优于以下解决方案。但是,在我自己的应用程序中,我希望将博客保留在与Rails站点不同的VPS(内存问题的分离)上。要使用户看到相同的URL,请使用通常用于代理杂种群集的代理技巧,除了代理另一盒上的端口80(或者其他端口)。十分简单。对用户而言,它就像代理mongrel一样透明-它们仅"看到" NGINX在我们域的端口80上进行响应。
upstream myBlogVPS { server 127.0.0.2:80; #fix me to point to your blog VPS } server { listen 80; #You'll have plenty of things for Rails compatibility here #Make sure you don't accidentally step on this with the Rails config! location /blog { proxy_pass http://myBlogVPS; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
我们可以使用此技巧使Rails随便使用我们想要的任何服务器技术。直接代理到适当的服务器/端口,NGINX会将其隐藏在外部。此外,由于所有URL都指向同一个域,因此只要正确编写URL,就可以无缝集成基于PHP的博客,基于Python的跟踪系统和Rails应用程序。
上面的答案很好地解决了问题。
另一种FCGI将使用php-fpm。 Docs有点稀疏,但效果很好。