16

我有一个标准的 Rails 应用程序,在http://mydomain上运行 Nginx 和 Mongrel 。我需要在http://mydomain.com/blog上运行一个 Wordpress 博客。我的偏好是将博客托管在 Apache 中,在同一台服务器或单独的机器上运行,但我不希望用户在 URL 中看到不同的服务器。这可能吗?如果没有,您会建议什么来实现目标?

4

5 回答 5

8

实际上,由于您使用的是 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 脚本。

于 2008-09-18T02:39:20.403 回答
6

我认为 joelhardi 的解决方案优于以下解决方案。但是,在我自己的应用程序中,我喜欢将博客保存在与 Rails 站点不同的 VPS 上(内存分离问题)。为了让用户看到相同的 URL,您使用通常用于代理到 mongrel 集群的相同代理技巧,除了您代理到另一个盒子上的端口 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 都指向同一个域,因此您可以无缝集成基于 PHP 的博客、基于 Python 的跟踪系统和 Rails 应用程序——只要您正确编写 URL。

于 2008-09-18T08:19:15.110 回答
2

上面的答案很好地解决了您的问题。

另一种 FCGI 是使用 php-fpm。文档有点稀疏,但效果很好。

于 2009-04-08T11:52:14.613 回答
1

如果您在 EC2 / AWS 环境中, Nginx 现在提供了执行此操作的脚本。

它可能很容易适应您的情况。这很方便。

于 2012-09-02T04:29:08.727 回答
0

在我看来,像重写操纵器之类的东西会做你想做的事。对不起,我没有更多的细节——只是大声思考:)

于 2008-09-18T02:34:50.787 回答