2012-12-22 32 views
11

我有一个特殊的URI方案,导致我一些麻烦。我需要运行的NodeJS服务如下:nginx + nodejs + php

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

我有这方面的工作使用express.vhost()担任了子域没有任何问题。 不过,我需要提供静态内容和PHP一旦URI类似于以下内容:

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

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

这种类型的方案可能与(nginx/php)/ nodejs配置?

+0

它现在有道理,我回到我的电脑时尝试这个。我遇到了403错误,试图访问expressjs路由。我想'break'声明解释了这一切。 –

回答

19

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

  • 传递给node.js后端
  • 传递给php-fpm后端
  • 替代路线传递给一个典型的Apache + mod_php的后端
  • 另一路由的缺省路由在nginx机器上获得js,图像,css和其他文件?直接服务于他们以最快的方式nginx的

我很喜欢,而且我认为这是对大多数发行版默认的设置布局,有conf.dvhosts.d目录与activeavailable文件夹。所以我可以通过简单地删除符号链接来轻松禁用虚拟主机或配置文件。

/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; 
    } 
} 

创建符号链接设置为默认虚拟主机主动

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

见nginx的配置语言多么的简单,直观的是什么?我只是喜欢它:)

+0

非常有帮助和信息!感谢您的多个例子。我开始看到nginx究竟有多灵活了!节日快乐! – grep