繁体   English   中英

如何配置Nginx以与Node.js和PHP一起使用

[英]How to configure nginx to work with Node.js and PHP

我在配置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脚本并向用户显示输出

因此,我想使用诸如node.js的PHP服务器在后台工作来执行某些特殊任务。 我不希望节点服务器位于子域上,我需要它一直运行,而不是针对特定请求运行。

我的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上吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM