php - nginx + nodejs + php

标签 php node.js nginx reverse-proxy

我有一个特殊的 URI 方案给我带来了一些麻烦。我需要运行 nodejs 来提供以下服务:

domain.com
var.domain.com
var.domain.com/foo/

我使用 express.vhost() 为子域提供服务没有问题。 但是,一旦 URI 类似于以下内容,我需要提供静态内容和 php:

var.domain.com/foo/bar
var.domain.com/foo/bar/index.php

这里,/bar/ 是我服务器上的某个目录。从该 url 开始的所有内容(例如 /bar/images/favicon.ico)都将像您的典型目录方案一样服务。通常我会对在某个端口上运行的 Node 执行典型的 proxy_pass,但是正如您在此处看到的,我需要 nodejs 作为端口 80 上的主要处理程序,并将请求传递给在其他端口上运行的 nginx(或者反过来可能/更简单?)。

这种类型的方案是否可以通过 (nginx/php)/nodejs 配置实现?

最佳答案

Nginx 允许非常灵活的请求路由。 我会告诉你一种设置方法

  • 传递给 node.js 的默认路由后端
  • 另一条路由传递给 php-fpm后端
  • 传递给典型 apache + mod_php 后端的替代路由
  • 在nginx机器上得到了js、图片、css等文件?直接从 nginx 以最快的方式为他们提供服务

我喜欢,我认为这是大多数发行版的默认设置布局,具有 conf.dvhosts.d 目录与 activeavailable 文件夹。因此,我只需删除符号链接(symbolic link)即可轻松禁用虚拟主机或配置文件。

/etc
     nginx.conf
     vhosts.d/
          active
          available
     conf.d/
          active
          available

/etc/nginx.conf

# should be 1 per CPU core    
worker_processes        2;                         

error_log               /var/log/nginx/error.log;

# I have this off because in our case traffic is not monitored with nginx and I don't want disks to be flooded with google bot requests :)
access_log              off;
pid                     /var/run/nginx.pid;

events {
        # max clients = worker_processes * worker_connections
        worker_connections      1024;
        # depends on your architecture, see http://wiki.nginx.org/EventsModule#use
        use                     epoll;
}

http {

        client_max_body_size    15m;

        include                 mime.types;
        default_type            text/html;
        sendfile                on;
        keepalive_timeout       15;

        # enable gzip compression
        gzip                    on;
        gzip_comp_level         6;
        gzip_types              text/plain text/css text/xml application/x-javascript application/atom+xml application/rss+xml application/json;
        gzip_http_version       1.0;


        # Include conf.d files
        include conf.d/active/*.conf;

        # include vhost.d files
        include vhosts.d/active/*.conf;
}

/etc/nginx/vhosts.d/available/default.conf

假设我们的静态文件的文档根目录是 /srv/www/vhosts/static/htdocs

server {
    server_name _;
    listen      80;

    root        /srv/www/vhosts/static/htdocs;

    # if a file does not exist in the specified root and nothing else is definded, we want to serve the request via node.js
    try_files   $uri    @nodejs;          

    # may want to specify some additional configuration for static files
    location ~ \.(js|css|png|gif|jpg)
    {
         expires 30d;
    }

    location @nodejs
    {
         # say node.js is listening on port 1234, same host         
         proxy_pass  127.0.0.1:1234;
         break;
    }

    # just for fun or because this is another application, we serve a subdirectory via apache on another server, also on the other server it's not /phpmyadmin but /tools/phpMyAdmin
    location /phpmyadmin {
         rewrite /phpmyadmin(.*)$   /tools/phpMyAdmin$1;
         proxy_pass                 10.0.1.21:80;
         break;
    }

    # files with .php extension should be passed to the php-fpm backend, socket connection because it's on the same and we can save up the whole tcp overhead
    location ~\.php$
    {
         fastcgi_pass unix:/var/run/php-fpm.sock;
         include /etc/nginx/fastcgi_params;
         break;
    }
}

创建符号链接(symbolic link)以激活默认虚拟主机

ln -s /etc/nginx/vhosts.d/available/default.conf /etc/nginx/vhosts.d/active/.
/etc/init.d/nginx restart

看看 nginx 配置语言有多简单直观?我只是不得不爱它:)

关于php - nginx + nodejs + php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13999069/

相关文章:

javascript - 从标准输出读取

ruby-on-rails - Nginx 乘客集成模式总是需要 Passengerfile.json 文件?

javascript - 为什么 onmessage 监听器不处理初始 SSE 事件?

Nginx请求路径变量?

javascript - 在选择下拉列表中选择时限制表单中的输入数量

php - 在 laravel 插入查询中,我想在数组上实现一个 for 循环以一次性插入多条记录

javascript - 在回调中调用 node.js 中的辅助函数?

javascript - 如何在 Netsuite 中使用 Node.js?

PHP 表单验证并提交到另一个页面

PHP XML 在另一个元素之后(或之前)插入元素