Nginx install and configuration skills

Compile and installation

Get latest source code from http://nginx.org/en/download.html

Some 3rd-party modules here

tar xzf nginx-1.4.3.tar.gz
cd nginx-1.4.3
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/body --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-proxy-temp-path=/var/tmp/nginx/proxy --http-scgi-temp-path=/var/tmp/nginx/scgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid
make
make install

Some optional options for advanced features:

--with-http_gzip_static_module

Sending precompressed files with the .gz filename extension instead of regular files by gzip_static on.

--add-module=/path/to/echo-nginx-module

Compile with module for bringing the power of echo, sleep, time and more to nginx’s config file. see more

location /test {
  echo "uri = $uri";
  echo "args = $args";
  ...
}

Nginx compile issues

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre option.

[Solution]

yum install pcre-devel.x86_64

[Note]

If pcre already installed, but still cannot find the pcre references:

In this case, when compiling nginx against a custom compiled library, such as pcre, zlib and OpenSSL, you must use the options --with-cc-opt and --with-ld-opt

--with-cc-opt="-I/usr/local/pcre/include" --with-ld-opt="-L/usr/local/pcre/lib"

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl option.

[Solution]

yum install openssl.x86_64 openssl-devel.x86_64

Nginx configuration guidlines

First understand IfIsEvil

set customize header

Add customize header for upstream identify when development environment (add variable$hostname to http headers).

add_header X-Original-Via $hostname;

Fastcgi normalize SCRIPT_FILENAME

With fastcgi_split_path_info we can customize the $fastcgi_path_info and $fastcgi_script_name

fastcgi_split_path_info regex; Defines a regular expression that captures a value for the $fastcgi_path_info variable. The regular expression should have two captures: the first becomes a value of the $fastcgi_script_name variable, the second becomes a value of the $fastcgi_path_info variable. For example, with these settings

location ~ ^(.+\.php)(.*)$ {
   fastcgi_split_path_info       ^(.+\.php)(.*)$;
   fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
   fastcgi_param PATH_INFO       $fastcgi_path_info;
}

and the “/show.php/article/0001” request, the SCRIPT_FILENAME parameter will be equal to “/path/to/php/show.php”, and the PATH_INFO parameter will be equal to “/article/0001”.

Related Links: