Как настроить совместную работу PHP-FPM и Node.JS через Nginx?

Сайт на Angular состоит из пары страниц с php-кодом для того чтобы была авторизация. Остальная информация должна загружаться через socket.io. Socket.io должен обращаться к 4 разным портам на которых висит 4 скрипта node.js. При загрузке страницы все пути с socket.io загружаются с ошибкой 404.
Так же используется CDN Cloudflare.

Мой текущий конфиг Nginx:
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;
	}
}
  • Вопрос задан
  • 1874 просмотра
Решения вопроса 1
customtema
@customtema
arint.ru
Я сделал разные виртхосты, и все заработало.

Нода
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;
	} 
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@Ky6uk-Hy6uk
В моем случае работает вот с такой конфигурацией
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;
    }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы