【问题标题】:Running both PHP and Node.js on Nginx(php extension file for PHP, html extension for node)在 Nginx 上同时运行 PHP 和 Node.js(PHP 的 php 扩展文件,节点的 html 扩展)
【发布时间】:2016-03-06 06:06:17
【问题描述】:

美好的一天,我想在同一台服务器上处理 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 页面中的节点 javascript。如何在同一台服务器上同时处理 php 和 html 两种类型的文件?

【问题讨论】:

  • 当你打开 sample.html 会发生什么?第404章?
  • 页面打开但节点根本不处理javascript,如果打开example.com:8080/sample.html页面显示完美!

标签: javascript php html node.js nginx


【解决方案1】:

这样不行。

问题是:当您使用nginx+php 时,nginx 在您点击localhost/filename.php 时并不会真正运行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 日志(/var/log/nginx/* 在 Ubuntu/Debian 上)。 :)

【讨论】:

  • 已解决,您可以在这里找到解决方案:github.com/fastogt/siteonyourdevice/blob/master/node/…。 Nginx 根据需要工作,但是在开始加载 javascript 源时找不到它(我在浏览器调试模式下找到它“未定义参考 io”),然后我指定在哪里需要搜索 javascript 源
猜你喜欢
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2015-06-26
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多