Nginx使用NodeJS和Apache

时间:2014-09-26 13:50:10

标签: php node.js web-applications nginx

我有两台服务器:运行Nodejs Web应用程序和运行PHP Web应用程序的服务器B. 我想以这样的方式配置Nginx:当我键入http://www.example.com时,它会路由到nodejs Web应用程序 然后,如果我键入http://www.example.com/otherinternalpage,它将路由到PHP Web应用程序。

服务器A和B都在同一个内部网中,我在服务器A中安装了Nginx。

我赞成任何形式的帮助。 感谢

1 个答案:

答案 0 :(得分:1)

有可能,但我建议为每个应用程序使用不同的子域和ngnix配置文件,就像我为自己的项目所做的那样:

  • www.leave-management-system.org
  • demo.leave-management-system.org
  • influxdb.leave-management-system.org

优点是您可以在不影响其他应用程序的情况下禁用一个应用程序,并且在不同文件中配置更容易维护。

无论如何,你需要(按照链接):

  • 将nginx配置为反向代理。
  • 运行Nodejs forever
  • 使用PHP FPM将PHP作为FastCGI流程运行。

但是,如果您不想使用子域,但是在同一个域中使用两个不同的文件夹,则组合配置可能如下所示:

server { 
    listen 80 default;
    server_name www.example.com;
    ...

    location /otherinternalpage/  {
        try_files $uri $uri/ /index.php?/$request_uri;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        #This is a Linux socket config
        #fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        #Alternatively, you can configure nginx/PHP with a backend
        fastcgi_pass 127.0.0.1:{YOUR_PORT};
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME
            $document_root$fastcgi_script_name;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
    }

    location / {
        proxy_pass http://localhost:{YOUR_PORT};
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}