Nginx Installation and Configuration

From Biowikifarm Metawiki
Revision as of 16:11, 14 January 2013 by Andreas Plank (Talk | contribs) (rewrite, try_files: fix highlight)

Jump to: navigation, search

Based on


Installation

sudo nano /etc/apt/sources.list

add lines:

# necessary only for php-fpm, the php version for nginx:
deb http://packages.dotdeb.org stable all

Add the GnuPG key to your distribution:

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -
rm dotdeb.gpg

Install nginx, fpm, new php:

sudo apt-get update
sudo apt-get install php5 php5-fpm php-pear php5-common php5-mcrypt php5-mysql php5-cli php5-gd php5-curl php5-dev php5-imagick php5-imap php5-intl php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc
sudo apt-get install libapache2-mod-php5 php5-apc
sudo apt-get install nginx

Apply "cgi.fix_pathinfo = 0;" in php.ini (security, avoid loading undesired php in a subfolder).

Change php-fpm configuration with:

sudo nano /etc/php5/fpm/php-fpm.conf

and

sudo nano /etc/php5/fpm/pool.d/www.conf

setting:

pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 1500
request_terminate_timeout = 450s

Change nginx configuration with:

sudo nano /etc/nginx/nginx.conf

adding various settings.

edit the default vhost config:

cd /etc/nginx/sites-enabled; nano /etc/nginx/sites-available/default

(To create symlinks for further vhost files like "www.example.com" under sites-enabled:

ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.com

)


NOTE: One failure we experienced was that we tested php with the phpinfo.php file, which, however, uses short php open tags. The default FPM-based php.ini in /etc/php5/fpm has short_open_tag = Off however. This initially and wrongly lead us to the conclusion that php was not working.
NOTE: for testing we used port 8880, which goes through bgbm (but not jki) firewalls.


Restart apache, nginx and fpm (we still use apache for certain uses):

sudo /usr/sbin/apache2ctl -k graceful && sudo service nginx restart && sudo service php5-fpm restart

STOP:

sudo /etc/init.d/php5-fpm stop; sudo /etc/init.d/nginx stop

Configuration: Locations and Rewrites

See for instance /etc/nginx/sites-availabel/default

Cheat sheet

location

See http://wiki.nginx.org/NginxHttpCoreModule#location

Syntax:
location [ = | ^~ | ~ | ~* ] uri { ... }
location { } @ name { … }
Check order:
  location = string-uri { … }   further searching
  location ^~ string-uri { … }  further searching
  location ~  regex-uri { case sensiteve }   ┬ executed in order of appearence
  location ~* regex-uri { case insensiteve } ┘
  location string-uri { … }

The order in which location directives are checked is as follows:

(1) Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops.

(2) All "^~" prefixed locations with conventional strings. If it matches, searching stops.

(3) Regular expressions, in the order they are defined in the configuration file.

(4) All remaining directives with conventional strings. Most specific (=most detailed) are executed (line 2):

1 location /w/ { … }
2 location /w/images/details/ { … }
3 location /w/images/a/ { … }

rewrite, try_files

Examples

 1 location / {
 2   # Redirect domain-only access (= no path given, w/o or with /) to default wiki:
 3   rewrite "^[/]?$" /web/ permanent;
 4   # flag redirect at the end  → 302 is temporary redirect
 5   # flag permanent at the end → 301 is permanent redirect
 6   try_files $uri @do404; # if it fails try named location block @do404
 7 }
 8 location @do404 {   # try as wiki page:
 9   rewrite "^(.+)$" /web$1 redirect;  #(wiki reports 404 for non-existing pages! But can be created)
10 }