Nginx (简体中文)

From ArchWiki
Jump to navigation Jump to search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n links end


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 the June 2009 Web Server Survey from Netcraft, nginx now hosts nearly 3.37% of all domains worldwide, while lighttpd hosts about 1.8%. Nginx is now well known for its stability, rich feature set, simple configuration, and low resource consumption.


The nginx package is now in the [community] repository, the latest stable version is nginx-0.7.62:

 # pacman -S nginx

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



 # /etc/rc.d/nginx start

The nginx server should now be running. The default main page is /srv/http/nginx/index.html(the nginx package does not install a default index.html, you must create this now). After creating a /srv/http/nginx/index.html webpage, you can use this URL: to test if nginx is working.

To enable service by default at startup just add "nginx" to the DAEMONS in the /etc/rc.conf.


You can modify the configurations by editing the files in /etc/nginx/conf. (/etc/nginx/conf/nginx.conf being the main config file) .

More details can be referred from Nginx Configuration Examples.

Use PHP/Python with nginx

FastCGI technology is introduced into nginx to work with many external tools and servers, ie: 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服务器
There are two different ways to run FastCGI server:

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

Archlinux's php package in the [extra] repository has already enabled FastCGI support.

For example:

 $ sudo pacman -S php php-cgi
 $ 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 /etc/nginx/html which is moved outside the default nginx directory and symlinked to /srv/http/nginx in the arch package. For php to work with nginx 0.7.62 the open_basedir in /etc/php/php.ini has to contain the directory /etc/nginx/html. IE. open_basedir = /etc/nginx/html/:/srv/http/:/home/:/tmp/:/usr/share/pear/

Below is a daemon script to be used in rc.conf on boot to start php-cgi. save it as /etc/rc.d/fastcgi and add "fastcgi" to your DAEMONS array in /etc/rc.conf

 . /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}"

2) Running PHP inside some third-party wrapper - this can be more comfortable than first method because of more flexibility. You can install fcgi(which includes cgi-fcgi), lighttpd (which includes spawn-fcgi), and PHP-FPM (PHP FastCGI Process Manager, which is now in the AUR. Recommended.)

For example:

 # 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 /etc/php-fpm.conf at first. More details about php-fpm can be referred from php-fpm website

Note: Sometimes, you may encounter some permission issue when running some third party software to start PHP as FastCGI-server. See Tips.

第二步: 编辑配置文件 /etc/nginx/conf/nginx.conf
Un-comment these lines and edit in the file to be as follows:

 location ~ \.php$ {
      root           html;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params

Step 3: Restart nginx
If nginx has been working, run

 # /etc/rc.d/nginx restart

Edit /srv/http/nginx/index.php,


view the URL: http://localhost/index.php with your browser, and you'll find PHP has worked well with nginx.


  • allow/deny based on IP


  • HTTP authentication


  • HTTPS support



  • Tuning the kernel for better speed

Add the followings to /etc/sysctl.conf

 net.ipv4.tcp_fin_timeout = 30
 net.ipv4.tcp_keepalive_time = 300
 net.ipv4.tcp_syncookies = 1
 net.ipv4.tcp_tw_reuse = 1
 net.ipv4.tcp_tw_recycle = 1
 net.ipv4.ip_local_port_range = 5000 65000 

and make it active:

 # /sbin/sysctl -p
  • The browser results in: the page you are looking for is temporarily unavailable. Please try again later.

This is because the FastCGI server has not been started.

  • The browser results in: no input file specified

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's simply an permission 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 instance :

 $ sudo groupadd www
 $ sudo useradd -g www www
 $ sudo chmod +w /srv/www/nginx/html
 $ sudo chown -R www:www /srv/www/nginx/html
 $ sudo 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 nginx.conf, 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, don't define it in any location section.

Also keep in mind that your php script path was defined as /srv/www/nginx/html by default using the variable "open_basedir" in /etc/php/php.ini, you can change them if you need.