https://wiki.archlinux.org/api.php?action=feedcontributions&user=Zacharydenton&feedformat=atomArchWiki - User contributions [en]2024-03-28T18:29:28ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Nginx&diff=209367Nginx2012-06-15T15:18:07Z<p>Zacharydenton: Improve grammar in the opening sentences of == Configuring ==</p>
<hr />
<div>[[Category:Web Server]]<br />
[[ru:Nginx]]<br />
[[zh-CN:Nginx]]<br />
'''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 [http://news.netcraft.com/archives/2012/04/04/april-2012-web-server-survey.html 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.<br />
<br />
== Installation ==<br />
<br />
The {{Pkg|nginx}} package is in the [[Official Repositories|official repositories]].<br />
# pacman -Sy nginx<br />
<br />
{{note|For a '''Ruby on Rails''' oriented installation, see [[RubyOnRails#Option_C:_The_Perfect_Rails_Setup|The Perfect Rails Setup]].}}<br />
<br />
== Starting Service ==<br />
<br />
To start the Nginx service, run:<br />
# rc.d start nginx<br />
<br />
The default served page at http://127.0.0.1 is: <br />
/usr/share/nginx/http/index.html<br />
<br />
The {{Pkg|nginx}} package does not install a default {{ic|index.html}} file; you must create this file yourself, if you wish.<br />
<br />
To enable the Nginx service by default at start-up just add {{Ic|nginx}} to the {{Ic|DAEMONS}} array in {{ic|/etc/rc.conf}}:<br />
DAEMONS=(ntpd syslog-ng ... nginx)<br />
<br />
== Configuring ==<br />
<br />
You can modify the configuration by editing the files in {{ic|/etc/nginx/conf}}. The main configuration file is located at {{ic|/etc/nginx/conf/nginx.conf}}. <br />
<br />
More details can be found here: [http://wiki.nginx.org/NginxConfiguration Nginx Configuration Examples].<br />
<br />
=== FastCGI ===<br />
<br />
FastCGI, also FCGI, 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.<br />
<br />
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. <br />
<br />
==== PHP implementation ====<br />
<br />
There are different ways to run a FastCGI server for PHP.<br />
<br />
===== Step 1: PHP configuration =====<br />
<br />
The {{Ic|open_basedir}} in {{ic|/etc/php/php.ini}} has to list base directories which contain PHP files, like {{ic|/srv/http/}} and {{ic|/usr/share/webapps/}}:<br />
open_basedir = /usr/share/webapps/:/srv/http/:/home/:/tmp/:/usr/share/pear/<br />
<br />
===== Step 2, option A: php-fpm =====<br />
<br />
* http://php-fpm.org<br />
<br />
Install {{Pkg|php-fpm}}:<br />
# pacman -Sy php-fpm<br />
<br />
The configuration file is {{ic|/etc/php/php-fpm.conf}}.<br />
<br />
To start the service:<br />
# rc.d start php-fpm<br />
<br />
Add {{Ic|php-fpm}} to the {{Ic|DAEMONS}} array in {{ic|/etc/rc.conf}}.<br />
<br />
===== Step 2, option B: spawn-fcgi-php =====<br />
<br />
Install {{Aur|spawn-fcgi-php}}, available at [[AUR]]:<br />
$ yaourt -Sy spawn-fcgi-php<br />
<br />
The configuration file is {{ic|/etc/conf.d/spawn-fcgi-php.conf}}.<br />
<br />
To start the service:<br />
# rc.d start spawn-fcgi-php<br />
<br />
Add {{Ic|spawn-fcgi-php}} to the {{Ic|DAEMONS}} array in {{ic|/etc/rc.conf}}.<br />
<br />
===== Step 3: Nginx configuration =====<br />
<br />
Inside each {{Ic|server}} block serving a PHP web application should appear a {{Ic|location}} block similar to:<br />
location ~ \.php$ {<br />
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;<br />
fastcgi_index index.php; <br />
include fastcgi.conf;<br />
}<br />
'''Pay attention''' to the {{Ic|fastcgi_pass}} argument, as it must be the TCP or Unix socket defined by the chosen FastCGI server in its config file. The '''default''' Unix for {{Ic|php-fpm}} is<br />
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;<br />
and for {{Ic|spawn-fcgi-php}},<br />
fastcgi_pass unix:/var/run/spawn-fcgi-php.sock;<br />
. Or you may use the common TCP socket, '''not default''',<br />
fastcgi_pass 127.0.0.1:9000;<br />
. Unix domain sockets are however much better.<br />
<br />
{{Ic|fastcgi.conf}} or {{Ic|fastcgi_params}} are usually included because they hold FastCGI settings for Nginx; the use of the latter is deprecated, though. They come within the Nginx installation.<br />
<br />
Finally, if Nginx has been working, run:<br />
# rc.d restart nginx<br />
<br />
If you would like to test the FastCGI implementation, create {{ic|/srv/http/index.php}} with content<br />
<?php<br />
phpinfo();<br />
?> <br />
and visit the URL http://127.0.0.1/index.php with your browser.<br />
<br />
==== CGI implementation ====<br />
<br />
This implementation is needed for CGI applications.<br />
<br />
===== fcgiwrap =====<br />
<br />
Install {{Pkg|fcgiwrap}}:<br />
# pacman -Sy fcgiwrap<br />
. The default configuration sets TCP socket 127.0.0.1:9001 for listening. If you desire to use a Unix domain socket, edit {{ic|/etc/conf.d/fcgiwrap}} like this:<br />
SPAWNER='/usr/bin/spawn-fcgi'<br />
<br />
FCGI_SOCKET='/var/run/fcgiwrap.sock'<br />
FCGI_USER='http'<br />
FCGI_GROUP='http'<br />
FCGI_EXTRA_OPTIONS='-M 700'<br />
<br />
SPAWNER_ARGS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET $FCGI_EXTRA_OPTIONS -- /usr/sbin/fcgiwrap"<br />
. Start the service:<br />
# rc.d start fcgiwrap<br />
, and add {{Ic|fcgiwrap}} to the {{Ic|DAEMONS}} array in {{ic|/etc/rc.conf}}.<br />
<br />
Respectively, Nginx will need<br />
fastcgi_pass 127.0.0.1:9001;<br />
or<br />
fastcgi_pass unix:/var/run/fcgiwrap.sock;<br />
directives.<br />
<br />
; Multiple Workers<br />
<br />
You can increase the number of worker threads by adding the {{ic|-F <num>}} option to the {{ic|SPAWNER_ARGS}} variable in '''/etc/conf.d/fcgiwrap'''. Although this option will work, you may find it difficult to monitor the workers, because they have no parent process.<br />
<br />
A better way to handle multiple worker threads is to use the [https://aur.archlinux.org/packages.php?ID=60080 multiwatch] package from the [[AUR]]. After installing the package, add the following line near the top of the '''/etc/conf.d/fcgiwrap''' file:<br />
<br />
DAEMON='multiwatch'<br />
<br />
and modify the {{ic|SPAWNER_ARGS}} line:<br />
<br />
FCGI_CHILDREN=5<br />
SPAWNER_ARGS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET $FCGI_EXTRA_OPTIONS -- /usr/bin/multiwatch -f $FCGI_CHILDREN -- /usr/sbin/fcgiwrap"<br />
<br />
You'll find that the '''/etc/rc.d/fcgiwrap''' script now creates a '''/run/multiwatch.pid''' file instead of the old '''/run/fcgiwrap.pid''' file. The multiwatch daemon will take care of respawning children that die.<br />
<br />
== Troubleshooting ==<br />
<br />
=== Accessing local IP redirects to localhost ===<br />
<br />
Solution from the Arch Linux [http://bbs.archlinux.org/viewtopic.php?pid=780561#p780561 forum].<br />
<br />
Edit {{ic|/etc/nginx/nginx.conf}} and locate the "server_name localhost" line without a # infront of it, and add below:<br />
server_name_in_redirect off;<br />
<br />
Default behavior is that nginx redirects any requests to the value given as server_name in the config.<br />
<br />
=== Error: 403 (Permission error) ===<br />
<br />
This is most likely a permission error. Are you sure whatever user configured in the Nginx configuration is able to read the correct files?<br />
<br />
If the files are located within a home directory, (e.g. {{ic|/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), {{ic|/home/arch}} might be '''chmod 750''', simply {{Ic|chmod}} it to ''751'', and it should work.<br />
<br />
=== Error: 404 (Pathinfo error) ===<br />
<br />
In some framework (like thinkphp, cakephp) or CMS, they need the pathinfo function. <br />
<br />
1. Edit the file {{ic|/etc/php/php.ini}}, make sure<br />
cgi.fix_pathinfo=1<br />
2. Edit {{ic|/etc/nginx/conf/nginx.conf}}, comment<br />
<br />
location ~ \.php$ {<br />
...<br />
}<br />
<br />
to <br />
<br />
#location ~ \.php$ {<br />
#...<br />
#}<br />
<br />
Then add the follows,<br />
location ~ ^(.+\.php)(.*)$ {<br />
root /srv/http/nginx;<br />
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; <br />
#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.<br />
fastcgi_index index.php;<br />
set $document_root2 $document_root;<br />
if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }<br />
if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }<br />
if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }<br />
if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }<br />
if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }<br />
fastcgi_split_path_info ^(.+\.php)(.*)$;<br />
fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_script_name;<br />
fastcgi_param PATH_INFO $fastcgi_path_info;<br />
fastcgi_param PATH_TRANSLATED $document_root2$fastcgi_path_info;<br />
include fastcgi_params;<br />
fastcgi_param DOCUMENT_ROOT $document_root2;<br />
}<br />
<br />
=== Error: The page you are looking for is temporarily unavailable. Please try again later. ===<br />
<br />
This is because the FastCGI server has not been started. <br />
<br />
=== Error: No input file specified ===<br />
<br />
Most Likely you do not have the SCRIPT_FILENAME containing the full path to you scripts.<br />
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<br />
# spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi<br />
or you should create some group and user to start the php-cgi. For example:<br />
# groupadd www<br />
# useradd -g www www<br />
# chmod +w /srv/www/nginx/html<br />
# chown -R www:www /srv/www/nginx/html<br />
# spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/bin/php-cgi<br />
<br />
Another occasion is that, wrong "root" argument in the "location ~ \.php$" section in {{ic|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.<br />
<br />
Also keep in mind that your php script path was defined as {{ic|/srv/www/nginx/html}} by default using the variable "open_basedir" in {{ic|/etc/php/php.ini}}; you can change them if you need.<br />
<br />
== See Also ==<br />
* [[Nginx/Init_script|Init script for Nginx]]<br />
* [http://nginx.org/ Nginx Official Site]<br />
* [http://calomel.org/nginx.html Nginx HowTo]<br />
* [http://wiki.gotux.net/config:nginx Easy Config Files]</div>Zacharydenton