dongwu1992 2015-06-18 15:44
浏览 144

如何配置nginx以使用Node.js和PHP

I have problem with configuration nginx to work with Node.js and PHP.
Basically I want something like this:

  1. user open my-project.com
  2. node.js server is running on port 3001
  3. request from node.js is send to my-project.com on port 80 by http-proxy
  4. nginx (port 80) server run PHP scripts and display output to users

So I want to create something like PHP server with node.js working in background for some special tasks. I don't want node server on subdomain, I need it run for all time not for particular requests.

My nginx config

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);

With this I get Internal Server Error probably because I get in loop which redirects me to nginx then to node server and so on.

Any idea how can I make node server running on my-project.com and not nginx?

  • 写回答

1条回答 默认 最新

  • dq_1984 2015-06-18 16:13
    关注

    I am not sur (I begin with nginx), but you don't add a second configuration server to 127.0.0.1 and localhost to serve only 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;
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题