我有一个令我有些麻烦特定URI方案。 我需要运行的NodeJS服务如下:
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/
是我的服务器上的某个目录。 从该URL下(说的一切/bar/images/favicon.ico
)将成为像典型的目录方案。 通常我会做典型proxy_pass到节点中的某些端口上运行,但你可以看到这里,我需要的NodeJS是在端口80上的主要处理程序,并将它传递至其他端口上运行Nginx的请求(或想它有可能/简单其他各地的办法吗?)。
是这种类型的方案中可能具有(nginx的/ PHP)/配置的NodeJS?
Nginx的允许非常灵活的请求路由。 我会告诉你一个方法来设置
- 传递给默认路由的node.js后端
- 传递到另一条路线的php-fpm的后端
- 替代路线传递给一个典型的apache + mod_php的后端
- 有JS,图片,CSS和nginx的机器上的其他文件? 直接服务于他们的nginx的最快方法
我很喜欢,而且我认为这是对大多数发行版默认的设置布局,有conf.d
和vhosts.d
使用目录active
和available
文件夹。 所以,我可以很容易地通过简单地删除符号链接禁用虚拟主机或配置文件。
/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的配置语言是什么? 我只是喜欢它:)