Nginx

From ArchWiki
Revision as of 19:44, 18 May 2012 by Battocchia (Talk | contribs) (Work in progress!)

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 April 2012 Web Server Survey, Nginx now hosts 10.32% of all domains worldwide, while Apache hosts about 65.46%. Nginx is now well known for its stability, rich feature set, simple configuration, and low resource consumption.

Installation

The nginx package is in the official repositories.

# pacman -Sy nginx

For a Ruby on Rails oriented installation, see The Perfect Rails Setup.

Starting Service

To start the Nginx service, run:

# rc.d start nginx

The default served (i.e. the page served at http://127.0.0.1) page is:

/etc/nginx/html/index.html

(The nginx package does not install a default index.html file; you must create this file yourself, if you wish.)

After creating a /etc/nginx/html/index.html webpage, you can use this URL: http://127.0.0.1 to test if Nginx is working.

To enable the Nginx service by default at start-up just add nginx to the DAEMONS array in /etc/rc.conf:

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

Configuring

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 found here: Nginx Configuration Examples.

FastCGI

FastCGI is a protocol for interfacing interactive programs with a web server. FastCGI is a variation on the earlier Common Gateway Interface (CGI); FastCGI's main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page requests at once.

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

PHP Implementation

There are different ways to run a FastCGI server for PHP.

Step 1: Edit php.ini

The default internal $document_root for Nginx is /etc/nginx/html which is moved outside the default Nginx directory and symlinked to /srv/http in the Arch Linux package.

For PHP to work, the open_basedir in /etc/php/php.ini has to contain the directory /etc/nginx/html:

open_basedir = /etc/nginx/html/:/srv/http/:/home/:/tmp/:/usr/share/pear/.}}
Step 2, option A: php-fpm

Install php-fpm:

# pacman -Sy php-fpm

The configuration file is /etc/php/php-fpm.conf.

To start the service:

# rc.d start php-fpm

Add php-fpm to the DAEMONS array in /etc/rc.conf.

Step 2, option B: spawn-fcgi-php

Install spawn-fcgi-phpAUR:

$ yaourt -Sy spawn-fcgi-php

The configuration file is /etc/conf.d/spawn-fcgi-php.conf.

To start the service:

# rc.d start spawn-fcgi-php

Add spawn-fcgi-php to the DAEMONS array in /etc/rc.conf.


Step 3: Nginx configuration
 location ~ \.php$ {
      fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php; 
      include        fastcgi.conf;
 }

Step 3: Restart Nginx

If Nginx has been working, run:

# rc.d restart nginx

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

<?php
  phpinfo();
?> 

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 config.ru file in the source directory.

phpMyAdmin Setup

See the following wiki article: PhpMyAdmin#NGINX_Configuration.

Security

  • Allow/deny visitors based on IP:

ngx_http_access_module.

  • HTTP authentication:

ngx_http_auth_basic_module.

  • HTTPS support:

ngx_http_ssl_module.

Troubleshooting

Accessing local IP redirects to localhost

Solution from the Arch Linux forum.

Edit /etc/nginx/nginx.conf 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. /home/arch/public/webapp) 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), /home/arch 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 /etc/php/php.ini, make sure

cgi.fix_pathinfo=1

2. Edit /etc/nginx/conf/nginx.conf, comment

location ~ \.php$ {
...
}

to

#location ~ \.php$ {
#...
#}

Then add the follows,

location ~ ^(.+\.php)(.*)$ {
  root   /srv/http/nginx;
  fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock; 	
  #fastcgi_pass   127.0.0.1:9000; #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 127.0.0.1 -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 127.0.0.1 -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, do not 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.

See Also