Linux to build a nginx Web server tutorial

Source: Internet
Author: User
Tags bind html page openssl centos automake

below to introduce you to a Linux build Nginx Web server tutorial, if you do not want to use the Apache environment, want to use nginx environment can and small set together to see.

1, download Nginx

Command: wget

2, decompression

Command: Tar zxvf nginx-0.8.54.tar.gz

3, enter the catalogue

Command: CD nginx-0.8.54

4, install the dependent package

Command: yum-y install gcc pcre-devel OpenSSL openssl-devel (no network to find related RPM in CentOS)

5, implementation./configure


6. Continue to install




Make install

7. Start Nginx Service


8. Restart Nginx Service

Command:/usr/local/nginx/sbin/nginx-s Reload

9, modify the site configuration file

Command: vi/usr/local/nginx/conf/nginx.conf

10. Multi-Site Settings

⑴, create the Vhost directory under/usr/local/nginx/conf/

Command: Mkdir/usr/local/nginx/conf/vhost

⑵, create a file named linlik.conf in/usr/local/nginx/conf/vhost, write the site configuration file (see the site content below)

Command: vi/usr/local/nginx/conf/vhost/linlik.conf

⑶, open/usr/local/nginx/conf/nginx.conf file, add include in the appropriate location to include the above 2 files in

At the end of the page, add a sentence above the parentheses: include vhost/*.conf; Then save the exit and restart the Nginx service

11. Site Configuration document content for multiple sites

As follows:



Listen 80;

#listen [::]:80;


Index index.html index.htm index.php default.html default.htm default.php;


Include emlog.conf;

#error_page 404/404.html;

Location ~ [^/].php (/|$)


# comment try_files $uri = 404; To enable PathInfo

Try_files $uri = 404;

Fastcgi_pass Unix:/tmp/php-cgi.sock;

Fastcgi_index index.php;

Include fastcgi.conf;

#include pathinfo.conf;


Location ~. *. (gif|jpg|jpeg|png|bmp|swf) $


Expires 30d;


Location ~. *. (JS|CSS)? $


Expires 12h;


Access_log/home/wwwlogs/ access;


How to customize 404 pages under Nginx

The experience of customizing 404 pages Under IIS and Apache is already very much, Nginx is still relatively few, in order to solve their own problems specifically for this in-depth study. The results show that it is feasible and simple to configure a custom 404 page under Nginx, with just the following steps:

1. Create your own 404.html page

2. Change nginx.conf in the HTTP definition area add: fastcgi_intercept_errors on;

3. Change nginx.conf (or a separate Web site profile, such as a site profile under Nginx-> sites-enabled)

Add in the server area: Error_page 404/404.html or error_page 404 =

4. Restart Nginx After the change, test nginx.conf correctness:/opt/nginx/sbin/nginx–t

Errors such as #502 can be configured in the same way.

Error_page 502 503 504/50x.html;


1. You must add: fastcgi_intercept_errors on; If this option is not set, even if the 404.html is created and the Error_page is configured, there is no effect. Fastcgi_intercept_errors syntax: fastcgi_intercept_errors on|off default: fastcgi_intercept_errors off Add Location: http, server, Location by default, Nginx does not support custom 404 error pages, and only this instruction is set to On,nginx to support the redirection of 404 errors. It is important to note that the Fastcgi_intercept_errors On,nginx is not set to redirect the 404 error. In Nginx, the 404 error Redirection takes effect on the premise that fastcgi_intercept_errors on is set and the Error_page option (including syntax and corresponding 404 pages) is set correctly.

2. Do not for the sake of convenience or improve the weight of the home page to specify the first page as 404 error pages, and do not use other methods to jump to the home page.

3. A custom 404 page must be greater than 512 bytes, otherwise the IE default 404 page may appear. For example, imagine a custom 404.html with a size of 11 bytes (content: 404 error).

Nginx configuration installation and some errors that are often encountered

Nginx compile installation One, install Nginx must first install the appropriate compilation tool

Yum-y install gcc gcc-c++ autoconf automake

Yum-y install zlib zlib-devel OpenSSL openssl-devel

Set up Nginx Group

Groupadd-r Nginx

Useradd-s/sbin/nologin-g nginx-r Nginx

ID Nginx

Zlib:nginx provides gzip module, requires zlib library support

Openssl:nginx provides SSL functionality

Pcre: Support for address rewriting rewrite features

Nginx website Download Address: Version

Second, TAR-ZXVF nginx-1.2.8.tar.gz

Third, CD nginx-1.2.8


















Or use the default direct./configure

Compiling and installing

V, make && make install

After compiling, make install to install and then pull.

Summary: CentOS does not have the make compiler installed

Resolved: yum-y install gcc automake autoconf libtool make

Restart command/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf more references Nginx--help

The configuration of Nginx and common minor problems are as follows:

Nginx: [Emerg] bind () to failed (98:address already in use) Error resolution

Nginx: [Emerg] bind () to failed (98:address already in use)

Nginx: [Emerg] bind () to failed (98:address already in use)

Nginx: [Emerg] bind () to failed (98:address already in use)

Nginx: [Emerg] bind () to failed (98:address already in use)

Nginx: [Emerg] still could not bind ()

Problem Description: Address is already in use. This error may occur because the Nginx service card is dead and the port is occupied.

Solution: First use lsof:80 to see the 80 port by what program occupy. The results of the lsof return are as follows:


Nginx 3274 root 6u IPv4 10664 0t0 TCP *:http (LISTEN)

Nginx 3547 nginx 6u IPv4 10664 0t0 TCP *:http (LISTEN)

Discovery is Nginx program, so we put the Nginx service k off, restart the service. The order is as follows:

Kill-9 3274

Kill-9 3547

or killall-9 Nginx.

Start/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf The new load configuration file

Startup succeeded but an error message was found

[Warn]: 51200 worker_connections are more than open file resource

Although not a fatal problem does not affect the nginx run but it looks annoying. Let's fix this.

NGINX.CONF Configuration Issues

Events {

Use Epoll;

Worker_connections 51200; Here's the problem


The problem is the maximum number of file limits for Linux. Modify the number of Linux files limit ulimit-n 51200

[Root@localhost ~]# Ulimit-n

[Root@localhost ~]#

Next load the configuration file reboot ok ....

Nginx error_log error Log configuration instructions

The Nginx error_log types are as follows (from left to right: Debug most detailed crit least):

[Debug | info | notice | warn | error | crit]

For example: Error_log logs/nginx_error.log crit;

Explanation: Log files are stored in the Nginx installation directory of the Logs/nginx_error.log, the error type is crit, that is, logging the least error information;

Note Error_log off does not turn off logging, which writes log files to a file with a file name of off, and if you want to turn off error logging, use the following configuration:

Error_log/dev/null Crit;

Set the storage location to the Linux black hole

Also note the 0.7.53 version, Nginx will use the compiled default log location before reading the error log path specified by the configuration file, and if the user running Nginx does not have write access to that location, Nginx will output the following error:

[Alert]: Could not open error log file:open () "/var/log/nginx/error.log" failed (13:permission denied) Log_not_found syntax: Log_not_found on | Off

Default value: On

Working with Fields: location

This parameter specifies whether a log of 404 errors in the request for the client is logged, typically for robots.txt and favicon.ico files that do not exist, for example: location =/robots.txt {log_not_found off;

Finally: When all nginx configurations change, it is best to use the following command to test the configuration for errors and then use the-s reload overload



1, # represents root permissions, do not enter

2, the above is the default installation path of Nginx, if changed to the corresponding modification oh, for example, WDCP lanmp one-click installation package if you need to use the following command

#/www/wdlinux/nginx/sbin/nginx-s Reload

After entering, if prompted as follows, the configuration is correct:

Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is OK

Nginx:configuration file/usr/local/nginx/conf/nginx.conf Test is successful

Then use the overload command to allow Nginx to smooth reload the configuration without affecting normal access:

#/usr/local/nginx/sbin/nginx-s Reload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.