2012-12-22 59 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