Day17 --- a lightweight, high-performance server --Nginx

Nginx basis

  A, nginx introduction

    Description: Nginx is a high-performance HTTP server and reverse proxy, also a IMPA / POP3 / SMTR proxy server.

  Second, compile and install nginx

    1. First install PRCE (PRCE is to support nginx rewriting)

      To the installation directory: cd / usr / local / src /

      Download PRCE: wget HTTP : //downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

      Decompression installation package: the tar zxvf PCRE - 8.35 . The tar . GZ

      Into the package directory: cd PCRE-8.35

      Checking for dependencies: ./ Configure  

         Note: This time may be an error, not installed gcc, this is the system lacks c ++ environment, this time need to be installed:yum install -y gcc gcc-c++

      Compile: make

      Installation: make install 

         Note: generally one step: make && make install

      View Version: pcre-config --version    

        Note: If you can view the version, prove installed correctly.

    2. Install Nginx

      To the installation directory:  CD / usr / local / the src /

      Download nginx: wget HTTP://nginx.org/download/nginx-1.6.2.tar.gz

      Extracting installation package: tar zxvf nginx-1.6.2.tar.gz

      Into the package directory: cd / usr / local / nginx

      Checking for dependencies: ./configure --prefix = / usr / local / nginx

      Compile install: make && make install

  

    View nginx version: / nginx / sbin / nginx -v Note: This directory is nginx nginx installation directory.
        Note: the ability to view the version prove installed correctly
    3. Start nginx
    run directly / ngin / sbin / nginx nginx run directly in the main directory of nginx on the line
       Note: There may be an error, because port 80 is occupied. To determine all your apahce or other program does not take up your port 80.
          And 80-port depends on whether, if the barrier is perhaps the firewall. Turn off the firewall: service iptalbes stop

    4. Stop nginx

     ./sbin/nginx -s quit

    5. Is the test configuration file errors

    ./sbin/nginx -t

      

   6. View Help

    ./sbin/nginx -h

      

  Three, nginx control signal

    Format: kill - signal names nginx main process ID

      Signal name: TERM, INT forcibly ended, generally do not

          QUIT elegant End Process

          HUP reload the configuration file

          USR1 reread the log, with at time of split logs, when you need to change the purpose of the log is written, this requires the signal under execution.

          USR2 smooth upgrade, upgrade nginx time, use

          WINCH with USR2 apply, shut down the old process

  Five, nginx configuration files Detailed

      The configuration file structure:

            

  Note: Here in: sever inherited main, location inherit sever.

  Detailed structure:

      1.main (global block): Effect nginx overall instruction. Generally run nginx user group, the main process nginx Pid storage path, the profile is introduced, the number of work process allows the generation and the like.        

              Note: nginx each instruction by the end of the semicolon.
              #user administrator administrators; # configure the user or group, the default is nobody nobody.
              #worker_processes 2; # allow the process to generate the number, the default is 1
              # PID /nginx/pid/nginx.pid; # specify nginx process runs file storage address
              error_log log / error.log debug; # formulate log path level. This setting can be put into global block, http blocks, server block, level on this: debug | info | notice | warn | error | crit | alert | emerg

      2.event块:配置影响nginx服务器或者用户的网络连接。有每个进程的最大连接数、选择哪种事件模型驱动处理请求、是否允许同时接受多个网络连接、开启多个网络连接序列化等。     

              events {
                accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
                multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
                #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
                worker_connections 1024; #最大连接数,默认为512
              }

      3.http块:可以嵌套多个sever。配置代理、缓存、日志定义等绝大多数功能和第三方模块的配置。如:文件引入、mime-type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单链接请求等。      

              http {
                include mime.types; #文件扩展名与文件类型映射表
                default_type application/octet-stream; #默认文件类型,默认为text/plain
                #access_log off; #取消服务日志
                log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
                access_log log/access.log myFormat; #combined为日志格式的默认值
                sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
                sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
                keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。

                upstream mysvr { #注意:这里upstream mysver不会继承其他设置,也不会被继承。
                  server 127.0.0.1:7878;
                  server 192.168.10.121:3333 backup; #热备
              }

      4.sever块:配置虚拟主机的相关参数、一个http可以有多个虚拟主机。              

              http {
                server 127.0.0.1:7878;
                server 192.168.10.121:3333 backup; #热备
                }
                error_page 404 https://www.baidu.com; #错误页
                server {
                keepalive_requests 120; #单连接请求上限次数。
                listen 4545; #监听端口
                  server_name 127.0.0.1; #监听地址
                    location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
                      #root path; #根目录
                    #index vv.txt; #设置默认页
                    proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
                    deny 127.0.0.1; #拒绝的ip
                    allow 172.18.5.54; #允许的ip
                    }
                                                         } 

              }

      5.location块:请求的路由以及各种页面处理情况。             

              http { 
                server 127.0.0.1:7878;
                server 192.168.10.121:3333 backup; #热备
                }
                error_page 404 https://www.baidu.com; #错误页
                server {
                keepalive_requests 120; #单连接请求上限次数。
                listen 4545; #监听端口
                  server_name 127.0.0.1; #监听地址 
                    location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
                      #root path; #根目录
                      #index vv.txt; #设置默认页
                      proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
                      deny 127.0.0.1; #拒绝的ip
                      allow 172.18.5.54; #允许的ip 
                          } 
                                                            } 

                }

Guess you like

Origin www.cnblogs.com/kevinzr/p/12011998.html
Recommended