nginx nodejs php

前端之家收集整理的这篇文章主要介绍了nginx nodejs php前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个特定的URI方案,这使我有些麻烦.我需要运行nodejs来服务于以下内容

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

我有这个工作没有问题使用express.vhost()来提供子域名.
但是,一旦URI类似于以下内容,我需要提供静态内容PHP

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后端
>在Nginx机器上得到了js,images,css等文件?直接从Nginx为他们提供最快的方式

我喜欢,我认为这是大多数发行版的默认设置布局,具有包含活动和可用文件夹的conf.d和vhosts.d目录.所以我可以通过简单地删除符号链接来轻松地禁用vhost或配置文件.

/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配置语言的简单直观?我只是爱它:)

猜你在找的Nginx相关文章