nginx + nodejs + php

gre*_*rep 12 php reverse-proxy nginx node.js

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

domain.com
var.domain.com
var.domain.com/foo/
Run Code Online (Sandbox Code Playgroud)

我有这个工作没有问题express.vhost()用于提供子域.但是,一旦URI类似于以下内容,我需要提供静态内容和php:

var.domain.com/foo/bar
var.domain.com/foo/bar/index.php
Run Code Online (Sandbox Code Playgroud)

/bar/是我服务器上的某个目录.从那个url下来(比如说/bar/images/favicon.ico)的所有东西都会像你的典型目录方案一样.通常我会在某个端口上运行典型的proxy_pass到节点,但正如你在这里看到的,我需要nodejs作为端口80上的主要处理程序,并让它将请求传递给在其他端口上运行的nginx(或者反过来可能/更简单吗?).

这种类型的方案是否可以使用(nginx/php)/ nodejs配置?

Mic*_*eim 21

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

  • 传递给node.js后端的默认路由
  • 另一条路由传递给php-fpm后端
  • 替代路由传递给典型的apache + mod_php后端
  • 在nginx机器上获得js,images,css和其他文件?直接从nginx以最快的方式为他们服务

我喜欢,我认为这是大多数发行版的默认设置布局,包含conf.dvhosts.d目录activeavailable文件夹.因此,我只需删除符号链接即可轻松禁用vhost或配置文件.

/etc
     nginx.conf
     vhosts.d/
          active
          available
     conf.d/
          active
          available
Run Code Online (Sandbox Code Playgroud)

/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;
}
Run Code Online (Sandbox Code Playgroud)

/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;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建符号链接以使默认vhost处于活动状态

ln -s /etc/nginx/vhosts.d/available/default.conf /etc/nginx/vhosts.d/active/.
/etc/init.d/nginx restart
Run Code Online (Sandbox Code Playgroud)

看看nginx配置语言有多简单直观?我只是喜欢它:)