【问题标题】:How to configure nginx to work with Node.js and PHP如何配置 nginx 以使用 Node.js 和 PHP
【发布时间】:2015-09-04 08:47:49
【问题描述】:

我在配置 nginx 以使用 Node.js 和 PHP 时遇到问题。
基本上我想要这样的东西:

  1. 用户打开 my-project.com
  2. node.js 服务器在 3001 端口上运行
  3. 来自 node.js 的请求通过 http-proxy 发送到端口 80 上的 my-project.com
  4. nginx(80 端口)服务器运行 PHP 脚本并向用户显示输出

所以我想创建类似 PHP 服务器的东西,其中 node.js 在后台运行以完成一些特殊任务。我不希望子域上的节点服务器,我需要它一直运行而不是特定请求。

我的 nginx 配置

server {
   listen                *:80;

   server_name           my-project.com www.my-project.com;
   client_max_body_size 1m;

   root /var/www/public;
     index  index.html index.htm index.php;

   access_log            /var/log/nginx/nxv_5rxici0o7b9k.access.log;
   error_log             /var/log/nginx/nxv_5rxici0o7b9k.error.log;

   location / {
     proxy_pass http://localhost:3001;     ### I added this line
     root  /var/www/public;
     try_files $uri $uri/ /index.php$is_args$args;
      autoindex off;
     index  index.html index.htm index.php;

   }


   location ~ \.php$ {

     root  /var/www/public;
     fastcgi_index index.php;
     fastcgi_split_path_info ^(.+\.php)(/.*)$;
     try_files $uri $uri/ /index.php$is_args$args;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass 127.0.0.1:9000;

     fastcgi_param SCRIPT_FILENAME $request_filename;
     fastcgi_param APP_ENV dev;

   }
   sendfile off;
 }

server.js

var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();

app.get('/', function (req, res) {
    console.log("TEST!!");
    proxy.web(req, res, { target: 'http://127.0.0.1:80' });
    //res.send('Hello World!');
});

app.listen(3001);

有了这个,我得到 Internal Server Error 可能是因为我进入了循环,将我重定向到 nginx 然后到节点服务器等等。

知道如何让节点服务器在 my-project.com 而不是 nginx 上运行吗?

【问题讨论】:

  • 你能描述一下这个场景吗?从我的角度来看,没有必要使用 PHP 和 node.js 托管网站,为什么不完全使用这两个选项之一?

标签: javascript php node.js nginx


【解决方案1】:

我不是 sur(我从 nginx 开始),但是你不会在 127.0.0.1 和 localhost 中添加第二个配置服务器来仅服务 php 吗?

server {
  listen                *:80;

  server_name           127.0.0.1 localhost;
  client_max_body_size 1m;

  root /var/www/public;
    index  index.html index.htm index.php;

  access_log            /var/log/nginx/nxv_5rxici0o7b9k.access.log;
  error_log             /var/log/nginx/nxv_5rxici0o7b9k.error.log;

  location ~ \.php$ {

 root  /var/www/public;
 fastcgi_index index.php;
 fastcgi_split_path_info ^(.+\.php)(/.*)$;
 try_files $uri $uri/ /index.php$is_args$args;
 include /etc/nginx/fastcgi_params;
 fastcgi_pass 127.0.0.1:9000;

 fastcgi_param SCRIPT_FILENAME $request_filename;
 fastcgi_param APP_ENV dev;

 }
 sendfile off;
 }
server {
   listen                *:80;

   server_name           my-project.com www.my-project.com;
   client_max_body_size 1m;

   root /var/www/public;
     index  index.html index.htm index.php;

   access_log            /var/log/nginx/nxv_5rxici0o7b9k.access.log;
   error_log             /var/log/nginx/nxv_5rxici0o7b9k.error.log;

   location / {
     proxy_pass http://localhost:3001;     ### I added this line
     root  /var/www/public;
     try_files $uri $uri/ /index.php$is_args$args;
      autoindex off;
     index  index.html index.htm index.php;

   }


   location ~ \.php$ {

     root  /var/www/public;
     fastcgi_index index.php;
     fastcgi_split_path_info ^(.+\.php)(/.*)$;
     try_files $uri $uri/ /index.php$is_args$args;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass 127.0.0.1:9000;

     fastcgi_param SCRIPT_FILENAME $request_filename;
     fastcgi_param APP_ENV dev;

   }
   sendfile off;
 }

【讨论】:

    猜你喜欢
    • 2018-07-27
    • 2021-03-02
    • 2012-04-04
    • 1970-01-01
    • 2015-02-01
    • 2017-03-25
    • 2021-08-14
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多