Difference between revisions of "Nginx"

From ArchWiki
Jump to navigation Jump to search
(Troubleshooting)
m (Troubleshooting: Added redirect to localhost solution.)
Line 167: Line 167:
  
 
==Troubleshooting==
 
==Troubleshooting==
 +
 +
=== Accessing local IP redirects to localhost ===
 +
Solution from [http://bbs.archlinux.org/viewtopic.php?pid=780561#p780561 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)===
 
===Error: 403 (Permission error)===

Revision as of 19:48, 5 July 2010

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

Summary

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 May 2010 Web Server Survey, Nginx now hosts nearly 6.55% of all domains worldwide, while lighttpd hosts about 0.91%. Nginx is now well known for its stability, rich feature set, simple configuration, and low resource consumption.

Installation

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

 # pacman -S nginx

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

Starting service

Check that /etc/rc.d/nginx has this line correct:

PID="/var/run/nginx.pid"

Then, run:

 # /etc/rc.d/nginx start

To start the service, Nginx server should now be running.

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

/srv/http/nginx/index.html

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

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

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

DAEMONS=(syslog-ng hal .. 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 referred from Nginx Configuration Examples.

Use PHP/Python with Nginx

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 deamon, 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 NginX#Method_two_.28Third-party-wrapper.29.

Method one (PHP's built-in)

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 127.0.0.1:9000 &

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 Filename

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

 #!/bin/bash
 
 . /etc/rc.conf
 . /etc/rc.d/functions
 
 case "$1" in
   start)
 	stat_busy 'Starting Fastcgi Server'
 	if /usr/bin/php-cgi -b 127.0.0.1:9000 &
 	then
 		add_daemon fastcgi
 		stat_done
 	else
 		stat_fail	fi
 	fi
 	;;
   stop)
 	stat_busy 'Stopping Fastcgi Server'
 	[ -e /var/run/daemons/fastcgi ] && kill $(pidof php-cgi) &> /dev/null;
 	if [ $? -gt 0 ]; then 
 		stat_fail
 	else
 		rm_daemon fastcgi
 		stat_done
 	fi
 	;;
   restart)
 	$0 stop
 	$0 start
 	;;
   *)
 	echo "Usage: $0 {start|stop|restart}"
 esac
Method two (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 (Available via the AUR, recommended)

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 127.0.0.1 -p 9000 -C 9 -f /usr/bin/php-cgi
 # php --fpm -b 127.0.0.1:
Note: You may need to edit Template:Filename at first. More details on php-fpm can be found at the php-fpm website
Note: PHP-FPM will be included into Template:Filename.
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

Un-comment these lines and edit in the file to be as follows:

 location ~ \.php$ {
      root           html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php; 
      fastcgi_param  SCRIPT_FILENAME  /srv/http/nginx/$fastcgi_script_name;
      include        fastcgi_params
 }

Step 3: Restart nginx

If NginX has been working, run:

 # /etc/rc.d/nginx restart

Edit Template:Filename,

 <?php
   phpinfo();
 ?> 

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

Ruby (Rack-based and Rails)

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 Arch Linux. 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 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 requires a Template:Filename file in the source directory.

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 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 determent this), /home/arch might be chmod 750, simply chmod it to 751, and it should work.

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 don't 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's simply an permission 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 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 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, 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.

References