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

时间:2015-06-18 15:44:45

标签: javascript php node.js nginx

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

  1. 用户打开my-project.com
  2. node.js服务器正在端口3001上运行
  3. 来自node.js的
  4. 请求通过http-proxy
  5. 发送到端口80上的my-project.com
  6. nginx(端口80)服务器运行PHP脚本并向用户显示输出
  7. 所以我想创建类似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);
    

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

    任何想法如何让我的节点服务器在my-project.com上运行而不是nginx?

1 个答案:

答案 0 :(得分:0)

我不是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;
 }