Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx + nodejs + php

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/

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

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?).

Is this type of scheme possible with a (nginx/php) / nodejs configuration?

like image 639
grep Avatar asked Dec 22 '12 00:12

grep


People also ask

Can I use PHP with Nginx?

Create a PHP page in NginxAfter the restart, PHP is fully enabled on Nginx. To prove this, create a PHP file in Nginx's /var/www/html folder and test to ensure the page renders properly on the server. This creates the most basic PHP file outside of a “Hello World” example you could create.

Can I run Nodejs on Nginx?

For Nginx to route to the Node. js application listening on port 3000, we'll need to first unlink the default configuration of Nginx and then create a new configuration to be used for by our Node. js application. The Nginx configuration is kept in the /etc/nginx/sites-available directory.

How does Nginx communicate with PHP?

To accept FastCGI requests from NGINX, PHP-FPM can either listen on a TCP/IP socket or UNIX domain socket. Whichever address you choose to use is what NGINX uses to connect (proxy requests) to PHP-FPM, using the fastcgi_pass directive.

Is Nginx faster than node?

Dedicated reverse proxy tools, like Nginx and HAProxy, typically perform these operations faster than Node. js. Having a web server like Nginx read static content from disk is going to be faster than Node.


1 Answers

Nginx allows very flexible request routing. I will show you a way to set up

  • 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

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

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

See how simple and intuitive the nginx configuration language is? I just HAVE to love it :)

like image 123
Michel Feldheim Avatar answered Sep 21 '22 23:09

Michel Feldheim