T
T
tuxx2015-11-15 21:37:27
PHP
tuxx, 2015-11-15 21:37:27

How to set up PHP-FPM and Node.JS to work together through Nginx?

The site on Angular consists of a couple of pages with a php code for authorization. The rest of the information must be loaded via socket.io. Socket.io should access 4 different ports on which 4 node.js scripts hang. When loading the page, all paths with socket.io are loaded with a 404 error.
Cloudflare CDN is also used.
My current Nginx config:

upstream php-fpm
{
  server unix:/var/run/php5-fpm.sock;
}

server
{
  listen 80;	
  root /var/www/example.com/public_html;
  index index.php index.html index.htm;
  server_name example.com sofast.example.com *.example.com;
  rewrite ^([^.]*[^/])$ $1/ permanent;
  add_header	X-Frame-Options		"SAMEORIGIN";
  add_header	X-Content-Type-Options	"nosniff";
  gzip		on;
  gzip_disable	"msie6";
  gzip_comp_level	6;
  gzip_min_length	1100;
  gzip_buffers	16 8k;
  gzip_proxied	any;
  gzip_types	text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/javascript application/json application/xml+rss;

  location /
  {
    index index.php index.html index.htm;
    try_files $uri $uri/ =404;
          fastcgi_pass    php-fpm;
          include fastcgi_params;
        	fastcgi_split_path_info                 ^(.+?\.php)(/.*)?$;
          fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
          fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_script_name;
          set             $path_info              $fastcgi_path_info;
          fastcgi_param   PATH_INFO               $path_info;
          fastcgi_param   SERVER_SIGNATURE        nginx/$nginx_version;
          fastcgi_index   index.php;
  }

  location ~* .(html|js|css|htm|jpg|jpg|gif|png|woff|woff2|ttf|mp3|svg|xml|ico|conf|eot|json|log|yml|txt|steamd|otf)$ {
                root /var/www/example.com/public_html;
                error_page 404 = 404;
        }

# 4 порта для Node.js
  location /node8303
  {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://127.0.0.1:8303;
    proxy_redirect off;
  }

        location /node8304
        {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:8304;
                proxy_redirect off;
        }

        location /node8305
        {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:8305;
                proxy_redirect off;
        }

        location /node8306
        {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:8306;
                proxy_redirect off;
        }

  location ~* "/\.(htaccess|htpasswd)$"
  {
    deny all;
    return 404;
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Spiridonov, 2015-11-15
@tuxx

I made different virthosts and everything worked.
But yes

server {
    charset         utf8;
    source_charset  utf8;
    server_name site.ru www.site.ru;
    listen 0.0.0.0:80;
    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://127.0.0.1:8181/;
    proxy_redirect off;
    }
}

php5-fpm
server {
  root /home/user/path/www;
  index index.html index.php;
  error_log  /home/user/path/error.log;
  charset         utf8;
  source_charset  utf8;
  server_name site.ru www.site.ru;
  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    try_files $uri $uri/ /index.html /index.php;
  }	
  #error_page 404 /404.html;
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index  index.php;
               fastcgi_param  SCRIPT_FILENAME  /home/user/path/www$fastcgi_script_name;
               include fastcgi_params;
       }
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  location ~ /\.ht {
  deny all;
  } 
}

A
Alexander, 2015-11-16
@Ky6uk-Hy6uk

In my case it works with this configuration

location / {
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_redirect off;
       proxy_pass http://127.0.0.1:8080;
}
location ~ \.php$ {
        if (!-e $document_root$document_uri) {
                return 404;
        }
        fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~* ^.+\.(txt|jpg|jpeg|gif|mpg|mpeg|avi|png|css|swf|ico|zip|rar|sdt|js|bmp|wav|mp3|mmf|mid|vkp|sisx|sis|exe|jar|thm|nth|doc)$ {
        access_log off;
        expires    1w;
        add_header   Cache-Control  'public';
        try_files $uri @notfound;
    }
    location @notfound {
        proxy_pass http://127.0.0.1:8080$request_uri;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question