· 02 · nginx Advanced Configuration Management Service Optimization

Business scene commonly used function modules Nginx http Summary

ngx_ http_ core_ module

Including - some core http parameter configuration corresponding to the configuration of the HTTP Nginx block portion

ngx_ http _access_ module

Access control module to control user access to the site of Nginx

ngx_ http_ gzip_ module

A compression module for compressing the data returned Nginx belonging performance optimization module

ngx_ http_fastcgi_ module

FastCGI module, dynamic applications and associated modules, such as PHP

ngx_ http_ proxy_ module

proxy proxy module

ngx_ http_upstream_ module

Load balancing module, can achieve health check load balancing and node site

ngx_ http_ rewrite_module

URL rewrite module address

ngx_ http_ limit_conn_module

Limit the number of concurrent users and the number of connection request module

ngx_ http_ limit req module

The defined key request process rate limiting Nginx

ngx_ http_ log_ module

Chi access module 8, record Nginx customers in the specified format to access information such as Chi 8

ngx_ http_ auth_basic_module

Web authentication module, set up by the Web user account and password to access Nginx

ngx_ http_ ssl_ module

ssI module, for encrypting HTTP connection, such as httpts

ngx_ http _stub_ status_ module

Recording status information Nginx basic access module

Core configuration file nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

nginx in a server is a virtual host, into the domain name, port, ip three types of web hosting

Actual domain name-based virtual hosting

1, configure name-based nginx.conf

#diff nginx.conf nginx.conf.default # Some important documents it itself has a backup file

#egrep -v "^$|^#|..#" nginx.conf>nginx.conf2/egrep -v "^$|#" nginx.conf>nginx.conf2

 server {

        listen       80;

        server_name  www.ram.shop;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

2, create a corresponding domain name and site directory files

[Root @ moban conf] # mkdir ../html/www -p # html directory in the new folder and www server in the root html / www correspond

[root@moban conf]# echo "http://www.ram.shop" >../html/www/index.html

[root@moban conf]# cat ../html/www/index.html

http://www.ram.shop

3, check the syntax, and reload nginx file

[root@moban conf]#../sbin/nginx -t

nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx/conf/nginx.conf test is successful

[root@moban conf]# ../sbin/nginx -s reload

[root@moban conf]# ps -ef |grep nginx    

root      8078  1978  0 05:59 pts/0    00:00:00 grep nginx

[root@moban conf]# echo "192.168.2.60 www.ram.shop" >>/etc/hosts

[root@moban conf]# tail -1 /etc/hosts

192.168.2.60 www.ram.shop

The client hosts file and configuration

C:\Windows\System32\drivers\etc\hosts  添加192.168.2.60   www.ram.shop

 

4, configure multiple name-based virtual hosting

Add http area nginx.conf in a server area, it is to add a virtual host, modify the domain name and the corresponding folders, reload service, add the domain name in the hosts file, add dns configured in the client hosts file, over

Add the server area zm

   server {

        listen       80;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    server {

        listen       80;

        server_name  www.zm.com;

        location / {

            root   html/zm;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

Detect what file format is correct

Reload nginx service / sbin / nginx -s reload

Adding hosts

Browser Test

Based on the actual configuration of the virtual host ports

Modify the listening port for each virtual host

Modify nginx.conf

    server {

        listen       81;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    server {

        listen       82;

        server_name  www.zm.com;

Check the file format, restarting services, see the port

[root@moban nginx]# netstat -lntup |grep nginx

tcp        0      0 0.0.0.0:81                  0.0.0.0:*                   LISTEN      1666/nginx         

tcp        0      0 0.0.0.0:82                  0.0.0.0:*                   LISTEN      1666/nginx         

tcp        0      0 0.0.0.0:83                  0.0.0.0:*                   LISTEN      1666/nginx

 

test

 

Port not only conflict with existing services, you can freely change, in principle, it is greater than 1024 and less than 65535

Configuring real IP address-based virtual hosts

Operation is similar, nginx.conf files are modified, you have to add a few addresses to the hosts in advance, using less

 

 

 

Check the Web site is alive with script

Script content

#! / bin / bash 
# Check the web site is alive, adding regular tasks, check 3 times per minute 
#by in the authors Patrick 
. /etc/init.d/ Functions 
the Check () { 
  IF (($ A ==   " 0 " )) ; the then 
    Action " ! This URL: $ B IS Alive "   / bin / to true 
    Exit 0 
  the else 
    Action   " ! This URL: $ B IS Not Alive " / bin / to false 
    Exit. 1 
  Fi 
} 
main () { 
  B = $ . 1 
  wget - O / dev / null -q $ 1 
  A = `echo $?`
  check
  exit 0
}
main $1

test

nginx configuration combat common functions

Optimize nginx configuration file: master configuration and each virtual host configuration file separation

The nginx.conf the server module out into a directory, easy management, use include file in the main configuration file; place to declare a file on it

include wording

 

Configuration

#mkdir extra

#sed -n '11,22p' nginx.conf >extra/www.conf

#sed -n '23,34p' nginx.conf > extra/nginx.conf

#sed -n '35,46p' nginx.conf > extra/bbs.conf

#sed  -i '11,46d' nginx.conf

#sbin/nginx  -t

#sbin/nginx -s reload

nginx.conf file configuration

http {

    include       mime.types;

    include       extra/www.conf;

    include          extra/nginx.conf;

    include          extra/bbs.conf;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

}

Or use sed -i '10 i include extra / www.conf; \ ninclude extra / nginx.conf 'nginx.conf

[root@moban extra]# pwd

/application/nginx/conf/extra

[root@moban extra]# ls

bbs.conf  nginx.conf  www.conf

Alias ​​virtual host configuration

To set up a virtual host one or more domain names other than the main domain name, domain names corresponding to achieve user access to multiple functions with a virtual host site

 

Status functionality combat

 

ngx_ http _stub_ status_ module

Recording status information Nginx basic access module

nginx version: nginx/1.17.4[root@moban conf]# ../sbin/nginx -V

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module    <==有这个就对了

[root@moban conf]# cat extra/status.conf

##STATUS

server{

    listen 80;

    server_name status.ram.com;

    location / {

        stub_status on;

        access_log off;

    }

}

The main configuration file to add include information

include    extra/status.conf;

Add hosts to the window resolve status.ram.com

Check the syntax to restart the service

[root@moban conf]# ../sbin/nginx -t

root@moban conf]# ../sbin/nginx -s reload

Test: As the number of connections it will automatically count the number of connections

 

Active connections: 5 ==> link several activities are treated as five

server accepts handled requests
 76 76 63

The first server now showing processing from the start to nginx 76 connections

The second server to start from now represents nginx successfully processed a total of 76 connections are equal indicating that the request is not lost

The third server nginx representation from start to now handled 63 requests

Reading: 0 Writing: 1 Waiting: 4

nginx reads the header information of the number of clients

nginx returned to the client number of header information

The number of connections waiting to be processed

nginx increase in the error log configuration

error_log is the core function module parameters

Configure the primary configuration file, add the location of the error log on it

error_log  logs/error.log -->nginx.conf

 

Access log (access_log) arranged in the http label

ngx_ http_ log_ module

Chi access module 8, record Nginx customers in the specified format to access information such as Chi 8

 

 

The main configuration file to add log_format primary format, you can find out from the default file copy

http {

    include       mime.types;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       81;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

access_log logs/access_www.log main;

View original web logs, access it, in view the log file,

[root@moban logs]# cat access_www.log

192.168.2.2 - - [10/Oct/2019:02:44:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"

[root@moban logs]# curl www.ram.com:81

http://www.ram.com

[root@moban logs]# cat access_www.log

192.168.2.2 - - [10/Oct/2019:02:44:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"

192.168.2.60 - - [10/Oct/2019:02:44:54 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"

Polling access log cutting

#!/bin/sh

Dateformat= `date +%Y%m%d`

Basedir=" /applicat ion/nginx"

Nginxlogdir=$Basedir/logs"

Logname=”access www ”

[ -d $Nginxlogdir ] && cd $Nginxlogdir llexit 1

[ -f ${Logname} .1og ]|| lexit 1

/bin/mv  ${Logname).1og ${Dateformat)_${Logname}.1og

$Basedir/sbin/nginx -S reload

 

Guess you like

Origin www.cnblogs.com/unicornam/p/11645768.html