Skip to content

Instantly share code, notes, and snippets.

@helxsz
Last active October 19, 2017 19:34
Show Gist options
  • Save helxsz/7510781 to your computer and use it in GitHub Desktop.
Save helxsz/7510781 to your computer and use it in GitHub Desktop.
nginx.conf
The idea is to have nginx installed and node installed. I will extend this gist to include how to install those as well, but at the moment, the following assumes you have nginx 0.7.62 and node 0.2.3 installed on a Linux distro (I used Ubuntu).
In a nutshell,
1) nginx is used to serve static files (css, js, images, etc.)
2) node serves all the "dynamic" stuff.
So for example, www.foo.com request comes and your css, js, and images get served thru nginx while everything else (the request for say index.html or "/") gets served through node.
3) nginx listens on port 80. 4) node listens on port 8124 (for this example only. you can change this port for your node app).
So in your /etc/nginx/sites-available/default modify your location / stanza and add the second stanza of this code block:
location / { proxy_pass http://127.0.0.1:8124; #this is the ip:port where your node app runs root /var/www/yoursitename; expires 30d; #uncomment this is you want to name an index file: #index index.php index.html;
access_log off; }
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
root /var/www/yoursitename/public;
}
Note: I did not change the /etc/nginx/nginx.conf file. It is still the default nginx.conf from the nginx installation.
Now, restart nginx.
/etc/init.d/nginx restart
(Re)start your node app.
node /path/to/your/node/app.js
Navigate to your site and verify.
Enjoy blazing fast static files and blazing fast dynamic content.
upstream lb-subprint {
ip_hash;
server 192.241.180.249:3222 weight=10 max_fails=3 fail_timeout=30s; # Reverse proxy to machine-1
server 192.241.241.152:3222 weight=10 max_fails=3 fail_timeout=30s; # Reverse proxy to machine-2
}
server {
listen 80;
server_name www.subprint.com subprint.com;
access_log /var/log/nginx/nginx.access.subprint.log;
error_log /var/log/nginx/nginx_error.subprint.log debug;
location / {
proxy_pass http://lb-subprint; # Load balance the URL location "/" to the upstream lb-subprint
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
}
server {
listen 80;
server_name subprint.com;
access_log /var/www/subprint/logs/access.log;
error_log /var/www/subprint/logs/error.log;
root /var/www/subprint/server/public; # express serves static resources for subprint.com out of here
location / {
proxy_pass http://127.0.0.1:8124;
root /var/www/subprint/server;
access_log on;
}
#serve static assets
location ~* ^(?!\/).+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
expires max;
access_log off;
}
# the route for the wordpress blog
# unfortunately the static assets (css, img, etc.) are not being pathed/served properly
location /blog {
root /var/www/localhost/public;
index index.php;
access_log /var/www/localhost/logs/access.log;
error_log /var/www/localhost/logs/error.log;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
break;
}
if (!-f $request_filename) {
rewrite /blog$ /blog/index.php last;
break;
}
}
# actually serves the wordpress and subsequently phpmyadmin
location ~* (?!\/blog).+\.php$ {
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/localhost/public$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}
# This works fine, but ONLY with a symlink inside the /var/www/localhost/public directory pointing to /usr/share/phpmyadmin
location /phpmyadmin {
index index.php;
access_log /var/www/phpmyadmin/logs/access.log;
error_log /var/www/phpmyadmin/logs/error.log;
alias /usr/share/phpmyadmin/;
if (!-f $request_filename) {
rewrite /phpmyadmin$ /phpmyadmin/index.php permanent;
break;
}
}
# opt-in to the future
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment