Nginx + Node.js + PHP [英] nginx + nodejs + php
问题描述
我有一个特殊的URI方案,这给我带来了一些麻烦.我需要运行nodejs来提供以下服务:
I have a particular URI scheme that is causing me some trouble. I need to run nodejs to serve the following:
domain.com
var.domain.com
var.domain.com/foo/
使用express.vhost()
为子域提供服务时,我没有任何问题.
但是,一旦URI类似于以下内容,我就需要提供静态内容和php:
I have this working no problem using express.vhost()
to serve up the sub domains.
However, I need serve static content and php once the URI resembles the following:
var.domain.com/foo/bar
var.domain.com/foo/bar/index.php
在这里,/bar/
是服务器上的某个目录.从该URL下来的所有内容(例如/bar/images/favicon.ico
)都将像您的典型目录方案一样工作.通常,我会将典型的proxy_pass传递给在某个端口上运行的节点,但是正如您在此处看到的那样,我需要nodejs成为端口80上的主要处理程序,并将请求传递给在其他端口上运行的nginx(是否可以/更简单地解决?).
Here, /bar/
is some directory on my server. Everything from that url down (say /bar/images/favicon.ico
) would serve like your typical directory scheme. Normally I would do the typical proxy_pass to node running on some port, but as you can see here, I need nodejs to be the primary handler on port 80, and have it pass the request off to nginx running on some other port (Or would it be possible/simpler the other way around?).
(nginx/php)/nodejs配置是否可以使用这种类型的方案?
Is this type of scheme possible with a (nginx/php) / nodejs configuration?
推荐答案
Nginx允许非常灵活的请求路由. 我将向您展示一种设置方法
Nginx allows very flexible request routing. I will show you a way to set up
- 传递给 node.js 后端 的默认路由
- 另一条传递给 php-fpm 后端 的路由
- 传递给典型apache + mod_php后端的替代路由
- 在Nginx机器上获取了js,图像,css和其他文件?直接从nginx以最快的方式为他们提供服务
- a default route passed to a node.js backend
- another route passed to a php-fpm backend
- alternative route passed to a typical apache + mod_php backend
- got js, images, css and other files on the nginx machine? serve them the fastest way directly from nginx
我喜欢,并且我认为这是大多数发行版的默认安装布局,其中包含具有active
和available
文件夹的conf.d
和vhosts.d
目录.因此,我只需删除符号链接即可轻松禁用虚拟主机或配置文件.
I like, and I think that's the default setup layout for most distros, to have conf.d
and vhosts.d
directories with active
and available
folders. So I can easily disable a vhost or configuration file by simply deleting the symlink.
/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
Say our document root for static files is /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;
}
}
创建符号链接以激活默认虚拟主机
create a symlink to make the default vhost active
ln -s /etc/nginx/vhosts.d/available/default.conf /etc/nginx/vhosts.d/active/.
/etc/init.d/nginx restart
看看nginx配置语言多么简单直观?我只是喜欢它:)
See how simple and intuitive the nginx configuration language is? I just HAVE to love it :)
这篇关于Nginx + Node.js + PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!