Installation and configuration of "Linux beginner" Nginx (v)

Source: Internet
Author: User
Tags epoll sendfile


1, from the official website http://nginx.org/en/download.html Download stable version nginx-1.8.0.tar.gz

TAR-ZXVF nginx-1.8.0.tar.gz Extract to/usr/local/nginx

2 . Set the configuration information./configure--prefix=/usr/local/nginx, or do not perform this step, direct default configuration

(1) Possible error after execution: checking for OS
+ Linux 2.6.32-358.el6.x86_64 x86_64
Checking for C compiler ... not found
./configure:error:c compiler cc is not found

Missing GC, performing yum install gcc

(2) Another error:

./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=<path> option.
is due to the lack of Pcre library This is the HTTP Rewrite module, which is the URL static package
pcre-8.37. tar.gz can be uploaded. Download pcre-8.37.tar.gz from official website http://exim.mirror.fr/pcre/

#tar-ZXVF pcre-8.37.tar.gz

#cd pcre-8.37

#./configure
#make
#make Install

(3) in the above #./configure when there are errors:

Configure:error:You need a C + + compiler for C + + support.

Execute Yum install-y gcc gcc-c++

Once you have completed the above error resolution, you can continue:

#cd nginx-1.8.0
#./configure
#make
#make Install

3. installation directory after installation of Nginx is/usr/local/nginx The
creates a new proxy.conf in the Conf folder to configure some of the proxy parameters as follows:

#!nginx (-)
# proxy.conf
Proxy_redirect off;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr; #获取真实ip
#proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; #获取代理者的真实ip
Client_max_body_size 10m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;
Proxy_buffer_size 4k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create a new nginx.conf in the Conf folder to configure some of the proxy parameters as follows:

#定义Nginx运行的用户和用户组
user www www;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;

#进程文件
pid /var/run/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#工作模式与连接数上限
Events
{
#参考事件模型, use [kqueue | rtsig | epoll |/dev/poll | select | poll]; EPOLL model is Linux 2 .6 The high-performance network I/O model in the kernel, if running on FreeBSD, use the Kqueue model.
Use Epoll;
#单个进程最大连接数 (maximum number of connections = number of connections * processes)
Worker_connections 65535;
}

#设定http服务器
http
{
include mime.types; #文件扩展名与文件类型映射表
Default_type Application/octet-stream; Default file type
#charset utf-8; #默认编码
Server_names_hash_bucket_size; #服务器名字的hash表大小
Client_header_buffer_size 32k; #上传文件大小限制
Large_client_header_buffers 4 64k; #设定请求缓
Client_max_body_size 8m; #设定请求缓
Sendfile on; #开启高效文件传输模式 , the sendfile instruction specifies whether Nginx calls the Sendfile function to output the file, and for normal applications set to ON, if it is used for downloading applications such as disk IO heavy load applications, can be set to off to balance disk and network I/O processing speed, reducing the load on the system. Note: If the picture does not appear normal, change this to off.
AutoIndex on; #开启目录列表访问, the appropriate download server, closed by default.
Tcp_nopush on; #防止网络阻塞
Tcp_nodelay on; #防止网络阻塞
Keepalive_timeout; #长连接超时时间, in seconds

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

upstream blog.ha97.com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#虚拟主机的配置
Server
{
#监听端口
Listen;
#域名可以有多个, separate
server_name www.ha97.com ha97.com with spaces;
Index index.html index.htm index.php;
Root/data/www/ha97;
Location ~. *. (PHP|PHP5)? $
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
include fastcgi.conf;
}
#图片缓存时间设置
Location ~. *. ( GIF|JPG|JPEG|PNG|BMP|SWF) $
{
expires 10d;
}
#JS和CSS缓存时间设置
Location ~. *. ( JS|CSS)? $
{
expires 1h;
}
#日志格式设定
Log_format access ' $remote _addr-$remote _user [$time _local] "$request" '
' $status $body _bytes_sent " $http _referer "'
'" $http _user_agent "$http _x_forwarded_for ';
#定义本虚拟主机的访问日志
Access_log/var/log/nginx/ha97access.log access;

#对 "/" Enable reverse proxy
Location/{
Proxy_pass http://127.0.0.1:88;
Proxy_redirect off;
Proxy_set_header x-real-ip $remote _addr;
#后端的Web服务器可以通过X-forwarded-for Get the user real IP
proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
#以下是一些反向代理的配置, optional.
Proxy_set_header Host $host;
Client_max_body_size 10m; #允许客户端请求的最大单文件字节数
Client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
Proxy_ Connect_timeout 90; #nginx跟后端服务器连接超时时间 (proxy connection timeout)
Proxy_send_timeout, #后端服务器数据回传时间 (proxy send timeout)
Proxy_read_timeout; #连接成功后, Back-end server response time (proxy receive timeout)
Proxy_buffer_size 4k; #设置代理服务器 (nginx) buffer size for saving user header information
proxy_buffers 4 32k; #proxy_buffers缓冲区, The average web page is set below 32k
Proxy_busy_buffers_size 64k; #高负荷下缓冲大小 (proxy_buffers*2)
Proxy_temp_file_write_size 64k;
#设定缓存文件夹大小, greater than this value, the
}
will be passed from the upstream server

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}

#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4. After modifying the/usr/local/nginx/conf/nginx.conf configuration file, execute the following command to check if the configuration file is correct:

#/usr/local/nginx/sbin/nginx-t

If the screen displays the following two lines of information, the configuration file is correct:

The configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
The configuration file/usr/local/nginx/conf/nginx.conf was tested successfully

If you are prompted to unknown host, you can do it on the server: Ping www.baidu.com If you are also prompted unknown host, there are two possibilities:
A, the server does not set the DNS server address, see if the/etc/resolv.conf is set, if not add
B, firewall interception

5, the command to start Nginx

#/usr/local/nginx/sbin/nginx

At this point, enter the following command to view the Nginx main process number:

Ps-ef | grep "Nginx:master Process" | Grep-v "grep" | Awk-f ' {print $} '

6, Stop the Nginx command

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


"Summary of errors that occurred during startup":

1,/usr/local/nginx/sbin/nginx-t Check conf configuration error:

-BASH:CD:/usr/local/nginx/sbin/nginx:not a directory

Check for missing commands: LDD $ (Which/usr/local/nginx/sbin/nginx)

Results:

Linux-vdso.so.1 = (0x00007fffbc8a7000)
libpthread.so.0 =/lib64/libpthread.so.0 (0x0000003785c00000)
Libcrypt.so.1 =/lib64/libcrypt.so.1 (0x0000003788400000)
libpcre.so.1 = Not found
libcrypto.so.10 =/usr/lib64/libcrypto.so.10 (0x000000378a800000)
Libz.so.1 =/lib64/libz.so.1 (0x0000003786800000)
libc.so.6 =/lib64/libc.so.6 (0x0000003785400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003785000000)
libfreebl3.so =/lib64/libfreebl3.so (0x0000003788c00000)
libdl.so.2 =/lib64/libdl.so.2 (0x0000003785800000)
Solution: Ln-s/usr/local/lib/libpcre.so.1/lib64

To execute the results again:

    linux-vdso.so.1 =>  (0x00007fff899ff000)
    libpthread.so.0 =/ lib64/libpthread.so.0 (0x0000003785c00000)
    libcrypt.so.1 =/lib64/libcrypt.so.1 ( 0x0000003788400000)
    

Also error: Error while loading shared Libraries:libpcre.so.1:cannot open Shared object file:no such file or directory, meaning to find To libpcre.so.1 this module, which causes the boot to fail.

If it is a 32-bit system
Ln-s/usr/local/lib/libpcre.so.1/lib
If it is a 64-bit system
Ln-s/usr/local/lib/libpcre.so.1/lib64
And then it's OK to start the nginx.
/usr/local/webserver/nginx/sbin/nginx

2. [Emerg]: Bind () to 0.0.0.0:80 failed (98:address already on use)

is the Nginx repeat reboot. Own the port.  Solution killall-9 Nginx to kill the Nginx process and then restart the line. Service Nginx Restart

3. Common commands:

/usr/local/nginx/sbin/nginx-Parameters
-C: Use the specified configuration file instead of the nginx.conf in the Conf directory.
-T: The test configuration file is correct and it is important to detect if a modified configuration file has a syntax error when the configuration needs to be reloaded at run time.
-S Reload overload
-S Stop stop

Installation and configuration of "Linux beginner" Nginx (v)

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.