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

How to configure nginx to work with Node.js and PHP

本文关键字:Node js PHP nginx 何配置 配置      更新时间:2023-09-26

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

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

所以我想用node创建类似PHP服务器的东西.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;
 }

服务器.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 和本地主机以仅提供 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;
 }