简体   繁体   中英

How to configure nginx to work with Node.js and 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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