在Nginx上运行PHP和Node.js(PHP的PHP扩展文件,Node的html扩展文件)


Running both PHP and Node.js on Nginx(php extension file for PHP, html extension for node)

你好,我想在同一台服务器上处理php文件和html,我想让html文件处理8080端口上的节点,让php文件处理php5服务。我这样写nginx配置:

server {
    listen 80 default_server;
    root /home/examle/public_html;
    index index.php index.html index.htm;
    server_name www.examle.com examle.com;
    location ~ '.html$ {
        proxy_pass http://localhost:8080;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }
    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location ~ '.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+'.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

但当html页面打开时,nodejs并没有处理html页面中包含的nodejavascript。如何在同一台服务器上处理php和html这两种类型的文件?

它不会以这种方式工作。

问题是:当您使用nginx+php时,当您达到localhost/filename.php时,nginx并不会真正运行php filename.php。有php-fpm,nginx向它发送请求。然后,php-fpm运行实际的脚本,创建合适的$_SERVER$_GET等。

node的情况下,您没有任何node-fpm服务。Nginx不会运行node filename.html。您需要在为http连接提供服务的8080端口上设置真正的节点进程,因为nginx所做的只是将实际的http连接"传递"给http://localhost:8080,正如您所指定的那样。

了解如何使节点http服务器正常工作。

附言提示:经常查看nginx日志(Ubuntu/Debian上的/var/log/nginx/*)。:)