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

我遇到配置nginx与Node.js和PHP一起使用的问题。

基本上我想要类似的东西 这个:</ p>


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

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

    我的nginx配置</ p>

       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;
    nn location / {
    proxy_pass http:// localhost:3001; ###我添加了这行
    root / var / www / public;
    try_files $ uri $ uri / /index.php$is_args$args;
    autoindex off;
    index index.html index.htm index。 php;

    }

    位置〜\ .php $ {

    根/ 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;
    nn fastcgi_param SCRIPT_FILENAME $ request_filename;
    fastcgi_param APP_ENV dev;

    }
    sendfile off;
    }
    </ code> </ pre>

    server.js </ p>

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

    appa .get('/',function(req,res){
    console.log(“TEST !!”);
    proxy.web(req,res,{target:'http://127.0.0.1:80 '});
    // @。发送('你好世界!');
    });

    app.listen(3001);
    </ code> </ pre>

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

    我知道如何在my-project.com上运行节点服务器而不是nginx? </ p>
    </ div>

展开原文

原文

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?

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

1个回答



我不是sur(我从nginx开始),但是你没有将第二个配置服务器添加到127.0.0.1和localhost到 只提供php?</ p>

  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 ;

位置〜\ .php $ {

根/ var / www / public;
fastcgi_index index.php;
fastcgi_split_path_info ^(。+ \。php)(/。*)$; \ n try_files $ uri $ uri / /index.php$is_args$args;
include / etc / nginx / fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
nn fastcgi_param SCRIPT_FILENAME $ request_filename;
fastcgi_param APP_ENV dev;

}
sendfile off;
}

{
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;
nn location / {
proxy_pass http:// localhost:3001; ###我添加了这行
root / var / www / public;
try_files $ uri $ uri / /index.php$is_args$args;
autoindex off;
index index.html index.htm index。 php;

}

位置〜\ .php $ {

根/ 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;
nn fastcgi_param SCRIPT_FILENAME $ request_filename;
fastcgi_param APP_ENV dev;

}
sendfile off;
}
</ code> </ pre>
</ div>

展开原文

原文

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

duanlu1959
duanlu1959 它不起作用。 我收到内部服务器错误
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐