From ArchWiki
Revision as of 15:26, 4 February 2012 by Gavinhungry (Talk | contribs) (updated survey stats)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Nginx (pronounced "engine X") written by Igor Sysoev (Russia) in 2005, is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. According to Netcraft's January 2012 Web Server Survey, Nginx now hosts 9.63% of all domains worldwide, while Apache hosts about 64.91%. Nginx is now well known for its stability, rich feature set, simple configuration, and low resource consumption.

Option A: Installation

The Template:Package Official package is now in the official repositories.

Or, you can compile and install the latest development version with the ABS.

Starting Service

To start the Nginx service, run:

# rc.d start nginx

The default served (i.e. the page served at page is:


(The Template:Package Official package does not install a default Template:Filename file; you must create this file yourself, if you wish).

After creating a Template:Filename webpage, you can use this URL: to test if Nginx is working.

To enable the Nginx service by default at start-up just add nginx to the DAEMONS array in Template:Filename:

DAEMONS=(ntpd syslog-ng ... nginx)


You can modify the configurations by editing the files in Template:Filename. (Template:Filename being the main config file.)

More details can be found here: Nginx Configuration Examples.

PHP/Python Integration

FastCGI technology is introduced into Nginx to work with many external tools and servers, i.e.: PHP and Python. So, you cannot use PHP or Python service unless FastCGI server has been started.

PHP and Python can be run as FastCGI application and can process FastCGI requests from Nginx.

FastCGI is usually faster than implementations in e.g. Apache where PHP is loaded each time, with FastCGI the script is simply passed to the PHP daemon, and the appropriate value is returned and used by Nginx.

Step 1: Start the FastCGI server

There are two different ways to run FastCGI server. It is recommended to use a third party wrapper (the second method).

1st Method "New" (as of PHP 5.3.3)

PHP now contains a FastCGI server spawner that takes care of everything for you.

To get started, install Template:Package Official and then start the service:

# rc.d start php-fpm

Now, move to: Step 2.

Note: You can configure the number of servers in the pool and tweak other configuration options by editing the file Template:Filename. More details on php-fpm can be found on the php-fpm website.
1st Method "Old" (PHP's built-in)

Directly running PHP's built-in FastCGI server -- this method does not require any third party tools.

Arch Linux's Template:Package Official package in the official repositories has FastCGI support enabled already.

For example, install Template:Package Official and Template:Package Official, and then run:

$ php-cgi -b &

This command sets up the local machine as a FastCGI server using port 9000.

Note: Since NginX 0.7.62, the default internal $document_root is Template:Filename which is moved outside the default NginX directory and symlinked to Template:Filename in the Arch Linux package. For PHP to work with NginX 0.7.62 the open_basedir in Template:Filename has to contain the directory Template:Filename:
open_basedir = /etc/nginx/html/:/srv/http/:/home/:/tmp/:/usr/share/pear/.

Below is a daemon script to be used in Template:Filename to start PHP-cgi on boot. Save it as Template:Filename and add fastcgi to your DAEMONS array in Template:Filename

 . /etc/rc.conf
 . /etc/rc.d/functions
 case "$1" in
 	stat_busy 'Starting Fastcgi Server'
 	if /usr/bin/php-cgi -b &
 		add_daemon fastcgi
 		stat_fail	fi
 	stat_busy 'Stopping Fastcgi Server'
 	[ -e /var/run/daemons/fastcgi ] && kill $(pidof php-cgi) &> /dev/null;
 	if [ $? -gt 0 ]; then 
 		rm_daemon fastcgi
 	$0 stop
 	$0 start
 	echo "Usage: $0 {start|stop|restart}"
2nd Method (third party wrapper)

This can be more comfortable than the first method because of increased flexibility.

There are currently three options for deploying PHP on Nginx:

  • fcgi
    • Includes cgi-fcgi
  • lighttpd
    • Includes spawn-fcgi
  • PHP-FPM (Recommended: see above)

To spawn a process, issue one of the following commands (matching the package you installed):

# cgi-fcgi -start -connect localhost:9000 /usr/bin/php-cgi
# spawn-fcgi -a -p 9000 -C 9 -f /usr/bin/php-cgi
# php --fpm -b
Note: You may need to edit Template:Filename at first. More details on php-fpm can be found at the php-fpm website.
Note: Sometimes, you may encounter some permission issue when running some third party software to start PHP as FastCGI-server. See Nginx#Troubleshooting.

Step 2: Edit /etc/nginx/conf/nginx.conf

Add index.php after index.htm in this line: (example below has index.php already added, you need to add it to your file manually)

location / {
      root    html;
      index   index.html index.htm index.php;

Then uncomment these lines and edit in the file to be as follows:

 location ~ \.php$ {
      try_files $uri =404;
      root           html;
      fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php; 
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
Note: If you are not using Template:Package Official, you may want to change the fastcgi_pass value to, or whichever IP and port to which your server is configured to respond. Do not forget the semicolon at the end of the line.
Note: If you are planning to run applications that support file uploads (images, for example), be sure and include the try_files line in the above configuration, otherwise you may be exposed to a security risk by allowing arbitrary code execution. The short explanation for this behavior is that a properly crafted URI which ends in ".php", in combination with a malicious image file that actually contains valid PHP, can result in the image being processed as PHP. For more information on the specifics of this behavior, you may wish to review the information provided on Neal Poole's blog. If you are running FastCGI on another machine, this won't work and you should look at a better way to solve this.

Step 3: Restart Nginx

If Nginx has been working, run:

# rc.d restart nginx

Edit Template:Filename,


Visit the URL: http://localhost/index.php with your browser, and you will find PHP has worked well with Nginx.

Ruby Integration (Ruby on Rails and Rack-based)

The Ruby on Rails wiki has tutorials on configuring Ruby for Nginx. It is not recommended to use FastCGI to make Nginx work with Ruby anymore, although very well possible. Nginx and Passenger are a great toolkit, and this setup is extremely easy to achieve on Archlinux. It should be noted, however, that there are other alternatives, including Mongrel, Thin and Unicorn.

Setting up Nginx and Passenger

Install the Nginx Passenger package from the AUR, and you are ready to go.

For further information on setting up Nginx with Ruby, read the Nginx part of this tutorial.

Note: With Passenger you are also able to run other Rack-based applications, including Sinatra. Note, however, that these require a Template:Filename file in the source directory.

phpMyAdmin Setup

See the following wiki article: PhpMyAdmin#NGINX_Configuration.


  • Allow/deny visitors based on IP:


  • HTTP authentication:


  • HTTPS support:


Option B: Ruby on Rails Oriented-Installation

See The Perfect Rails Setup.


Accessing local IP redirects to localhost

Solution from the Arch Linux forum.

Edit Template:Filename and locate the "server_name localhost" line without a # infront of it, and add below:

server_name_in_redirect off;

Default behavior is that nginx redirects any requests to the value given as server_name in the config.

Error: 403 (Permission error)

This is most likely a permission error. Are you sure whatever user configured in the Nginx configuration is able to read the correct files?

If the files are located within a home directory, (e.g. Template:Filename) and you are sure the user running Nginx has the right permissions (you can temporarily chmod all the files to 777 in order to determine this), Template:Filename might be chmod 750, simply chmod it to 751, and it should work.

Error: 404 (Pathinfo error)

In some framework(like thinkphp, cakephp) or CMS, they need the pathinfo function.

1. Edit the file Template:Filename, make sure


2. Edit Template:Filename, comment

location ~ \.php$ {


#location ~ \.php$ {

Then add the follows,

location ~ ^(.+\.php)(.*)$ {
  root   /srv/http/nginx;
  fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock; 	
  #fastcgi_pass; #Un-comment this and comment "fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;" if you are not using php-fpm.
  fastcgi_index  index.php;
  set $document_root2 $document_root;
  if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
  if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") {	set $document_root2 $1$2; }
  if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") {	set $document_root2 $1$2; }
  if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") {	set $document_root2 $1$2; }
  if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") {	set $document_root2 $1$2; }
  fastcgi_split_path_info ^(.+\.php)(.*)$;
  fastcgi_param	SCRIPT_FILENAME	$document_root2$fastcgi_script_name;
  fastcgi_param	PATH_INFO	$fastcgi_path_info;
  fastcgi_param	PATH_TRANSLATED	$document_root2$fastcgi_path_info;
  include	fastcgi_params;
  fastcgi_param  DOCUMENT_ROOT      $document_root2;

Error: The page you are looking for is temporarily unavailable. Please try again later.

This is because the FastCGI server has not been started.

Error: No input file specified

Most Likely you do not have the SCRIPT_FILENAME containing the full path to you scripts. If the configuration of nginx (fastcgi_param SCRIPT_FILENAME) is all right, this kind of error means php fail to load the requestd script. Usually it is simply a permissions issue, you can just run php-cgi as root

# spawn-fcgi -a -p 9000 -f /usr/bin/php-cgi

or you should create some group and user to start the php-cgi. For example:

# groupadd www
# useradd -g www www
# chmod +w /srv/www/nginx/html
# chown -R www:www /srv/www/nginx/html
# spawn-fcgi -a -p 9000 -u www -g www -f /usr/bin/php-cgi

Another occasion is that, wrong "root" argument in the "location ~ \.php$" section in Template:Filename, make sure the "root" points to the same directory as it in "location /" in the same server. Or you may just set root as global, do not define it in any location section.

Also keep in mind that your php script path was defined as Template:Filename by default using the variable "open_basedir" in Template:Filename; you can change them if you need.

See Also