2012-12-22 9 views
11

私はいくつかの問題を引き起こしている特定のURIスキームを持っています。私は、次のようにサービスを提供するためにnodejsを実行する必要があります。nginx + nodejs + ph

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

私はこのサブドメインを提供するためにexpress.vhost()を使用しても問題を働いていません。

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

/bar/は私のサーバー上のいくつかのディレクトリです: はしかし、私は、URIは次のような後に静的コンテンツとPHPを果たす必要があります。そのURLからのすべてのもの(例えば/bar/images/favicon.ico)は、典型的なディレクトリ方式のように機能します。通常は私はいくつかのポートで実行されているノードへの典型的なproxy_passを行いますが、ここで見ることができるように、nodejsはポート80のプライマリハンドラになる必要があり、nginxにリクエストを渡します。それは他の方法の周りで可能/より簡単ですか?)。

このタイプのスキームは(nginx/php)/ nodejs設定で可能ですか?

+0

今私は私のコンピュータに戻ってくるとき、私はこれを試してみましょう。私はexpressjsルートにアクセスしようとすると、403エラーが発生しました。そして、私は '休憩'声明がそれをすべて説明すると思います。 –

答えて

19

Nginxでは、非常に柔軟なリクエストルーティングが可能です。 私はあなたに

  • node.jsバックエンド
  • 典型的なApacheの+ mod_phpをバックエンド
  • に渡さphp-fpmバックエンド
  • 代替ルートに渡された別のルートに渡されたデフォルトルートを設定する方法を示しますnginxマシンでjs、画像、css、その他のファイルを取得しましたか? activeavailableフォルダにconf.dvhosts.dディレクトリを持つように、nginxの

私が好きで、私はそれがほとんどのディストリビューションのデフォルト設定のレイアウトだと思うから直接彼らに最速の方法を提供しています。 symlinkを削除するだけで、仮想ホストや設定ファイルを簡単に無効にすることができます。

# 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.conf

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

/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