How to build an nginx WEB server in linux

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

1. Download nginx
Command: wget http://nginx.org/download/nginx-0.8.54.tar.gz
 
2. Decompress
Command: tar zxvf nginx-0.8.54.tar.gz
 
3. Enter the Directory
Command: cd nginx-0.8.54
 
4. Install the dependency package
Command: yum-y install gcc pcre-devel openssl-devel (no network can be found in centos rpm)
 
5. Execute./configure
Command:./configure
 
6. Continue installation
Command:
Make
And
Make install
 
7. Start the nginx service
Command:/usr/local/nginx/sbin/nginx
 
8. Restart the 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
(1) create a vhost directory under/usr/local/nginx/conf/.
Command: mkdir/usr/local/nginx/conf/vhost
 
(2) create a file named linlik. conf in/usr/local/nginx/conf/vhost to write the site configuration file (view the bottom site content)
Command: vi/usr/local/nginx/conf/vhost/linlik. conf
 
(3) open the/usr/local/nginx/conf/nginx. conf file and add include to the corresponding location to include the above two files.
Add "include vhost/*. conf" to the brackets at the end of the page, save and quit, and restart the nginx service.
 
11. Content of site configuration documents for multiple sites
As follows:
 
Server
 {
Listen 80;
# Listen [:]: 80;
Server_name jiahaolin.com www.111cn.net;
Index index.html index.htm index. php default.html default.htm default. php;
Root/www/jiahaolin;
 
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 12 h;
 }
 
Access_log/home/wwwlogs/jiahaolin.com. log access;
 }


How to customize the 404 page in NGINX

There have been a lot of articles about the experience of customizing the 404 page under IIS and APACHE, and NGINX is still relatively small at present. In order to solve their own problems, we have made in-depth research on this. The research results show that it is feasible to configure a custom 404 page in NGINX, and it is very simple, just a few steps:

1.create the secret 404.html page

2. Change nginx. conf and add fastcgi_intercept_errors on in the http definition area;

3. Change nginx. conf (or a separate website configuration file, for example, the site configuration file under nginx-> sites-enabled)

Add: error_page 404/404. Html or error_page 404 = http://www.xxx.com/404.html in the server region

4. Restart nginx after modification to test the correctness of nginx. conf:/opt/nginx/sbin/nginx-t

#502 and other errors can be configured in the same way.

Error_page 500 502 503 x.html;

Note:

1. You must add: fastcgi_intercept_errors on. If this token is not set, creating 404.html and configuring error_page does not work. 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 page, only this command is set to on, nginx supports 404 error redirection. Note that if fastcgi_intercept_errors on is set, nginx will redirect the 404 error. In nginx, the precondition for 404 error redirection to take effect is that fastcgi_intercept_errors on is set, and the error_page option is set correctly (including syntax and corresponding 404 page)

2. Do not set the homepage as the 404 error page for the purpose of saving time or increasing the homepage weight, or use other methods to jump to the homepage.

3. The custom 404 page must be larger than 512 bytes; otherwise, the default 404 page of IE may appear. For example, the parameter is set to 404.html and the size is only 11 bytes (the content is 404 error ).

Nginx configuration and installation and some common errors

Nginx compilation and installation 1. The corresponding compilation tool must be installed before nginx installation.
Yum-y install gcc-c ++ autoconf automake
Yum-y install zlib-devel openssl-devel pcre-devel
Create an nginx Group
Groupadd-r nginx
Useradd-s/sbin/nologin-g nginx-r nginx
Id nginx
Zlib: nginx provides the gzip module, which must be supported by the zlib library.
Openssl: nginx provides ssl functions
Pcre: Supports address rewrite.
Nginx official website Download Address: http://nginx.org/The latest version of http://nginx.org/download/nginx-1.5.2.tar.gz
2. tar-zxvf nginx-1.2.8.tar.gz
C. cd nginx-1.2.8

Configuration

IV../configure \
-- Prefix =/usr \
-- Sbin-path =/usr/sbin/nginx \
-- Conf-path =/etc/nginx. conf \
-- Error-log-path =/var/log/nginx/error. log \
-- Pid-path =/var/run/nginx. pid \
-- User = nginx \
-- Group = nginx \
With-http_ssl_module \
With-http_flv_module \
With-http_gzip_static_module \
-- Http-log-path =/var/log/nginx/access. log \
-- Http-client-body-temp-path =/var/tmp/nginx/client \
-- Http-proxy-temp-path =/var/tmp/nginx/proxy \
-- Http-fastcgi-temp-path =/var/tmp/nginx/fcgi \
With-http_stub_status_module
Or use the default direct./configure

 
Compile and install

V. make & make install

 
After the compilation is complete, make install and install the tool.

Conclusion: The make compiler is not installed in centos.
Solution: yum-y install gcc automake autoconf libtool make
Restart command/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx. conf for more information, see nginx -- help
Nginx configuration and common problems are as follows:

Nginx: [emerg] bind () to 0.0.0.0: 80 failed (98: Address already in use) error solution
Nginx: [emerg] bind () to 0.0.0.0: 80 failed (98: Address already in use)
Nginx: [emerg] bind () to 0.0.0.0: 80 failed (98: Address already in use)
Nginx: [emerg] bind () to 0.0.0.0: 80 failed (98: Address already in use)
Nginx: [emerg] bind () to 0.0.0.0: 80 failed (98: Address already in use)
Nginx: [emerg] still cocould not bind ()
Problem Description: The address is already in use. The nginx service may be stuck, causing Port occupation. This error occurs.
Solution: First, use lsof: 80 to check which program occupies port 80. Lsof returns the following results:
Command pid user fd type device size/OFF NODE NAME
Nginx 3274 root 6u IPv4 10664 0t0 TCP *: http (LISTEN)
Nginx 3547 nginx 6u IPv4 10664 0t0 TCP *: http (LISTEN)
It was found that it was an nginx program, so we dropped the nginx service k and restarted the service .. The command 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 from the newly loaded configuration file

Started successfully, but an error message is found.
[Warn]: 51200 worker_connections are more than open file resource limit: 51200
Although it is not a fatal problem and does not affect nginx running, it seems annoying to solve it.
Nginx. conf configuration problems
Events {
Use epoll;
Worker_connections 51200; // error
}
The cause is the maximum number of files in Linux. Modify Linux file count limit ulimit-n 51200

[Root @ localhost ~] # Ulimit-n

 

[Root @ localhost ~] #

Next, re-load the configuration file and restart OK ....

 

Nginx error_log error log configuration instructions

The error_log type of nginx is as follows (from left to right: debug has the least detailed crit ):
[Debug | info | notice | warn | error | crit]
For example: error_log logs/nginx_error.log crit;
Explanation: log files are stored in the logs/nginx_error.log Directory of the nginx installation directory. The error type is crit, that is, the minimum error information is recorded;

Note that error_log off does not disable the logging function. It writes the log file to a file named off. To disable the error logging function, use the following configuration:
Error_log/dev/null crit;
Set the storage location to the black hole of Linux

Similarly, in version 0.7.53, nginx uses the default log location compiled before reading the error log path specified in the configuration file. If the user running nginx does not have the write permission for this location, nginx will output the following error:
[Alert]: cocould 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
Field used: location
This rule specifies a error log for logging client requests. It is usually used in non-existent robots.txt and favicon. ico files, such as location =/robots.txt {log_not_found off ;}

Finally, when all nginx configurations change, it is best to use the following command to test whether the configuration is correct and then use-s reload
#/Usr/local/nginx/sbin/nginx-t
Note:
1. # represents the root permission. You do not need to enter
2. The above is the default installation path of nginx. If you change the installation path, you need to modify it accordingly. For example, if you need to use the following command to install the one-click lanmp installation package of wdcp:
#/Www/wdlinux/nginx/sbin/nginx-s reload
If the following prompt is displayed, 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 smoothly reload the configuration of Nginx without affecting normal access:
#/Usr/local/nginx/sbin/nginx-s reload

 

Related Article

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: info-contact@alibabacloud.com 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.