See which modules nginx supports

./configure --help | more
Copy the code
  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                 set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
Copy the code

/configure with indicates the module to be added, without indicates the module to be installed by default

Configure different log formats:

Parameter Description Example$remote_addrThe client address is 211.28.65.253$remote_userClient user name --$time_localAccess time and time zone 18/Jul/2012:17:00:01 +0800$requestRequest URI and HTTP protocol"GET/article - 10000. HTTP / 1.1 HTML"
$http_hostRequest the address, which is the address (IP or domain name) you entered in your browser www.wang.com 192.168.100.100$statusHTTP request status 200$upstream_statusThe upstream state 200$body_bytes_sentSend file content size 1547 to client$http_refererUrl redirect source: https://www.baidu.com/$http_user_agentUser terminal browser information"Mozilla / 4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident / 4.0; SV1; GTB7.0; . NET4.0 C;$ssl_protocolThe SSL version is TLSv1$ssl_cipherThe algorithm for exchanging data is RC4-SHA$upstream_addrAddress of the background upstream 10.10.10.100:80 where the service is actually performed$request_timeThe total time of the entire request is 0.205$upstream_response_timeUpstream response time 0.002 during requestCopy the code

Default log rules

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
Copy the code

The first argument after log_format, main, represents an alias for the log format

You can configure logging within the HTTP module, server, or Location (log format specified as main or other log format).

access_log  logs/host.access.log  main;
Copy the code
    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"';

    log_format  test '$http_user_agent --test'; access_log logs/access.log main; . server { listen 80; server_name localhost; access_log logs/host.access.logtest; . }...Copy the code

Above we have added a new log format called test

If both HTTP and server are configured with access_log logging, the innermost part overrides the external access_log configuration.

At this point, access will get the configuration log format template for Test

Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36 --testCopy the code

Several instructions about proxy

location / {
          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://10.10.11.11:8088;
        }
Copy the code

The proxy_pass directive is forwarded to http://10.10.11.11:8088

As a reverse proxy, which may be followed by a service cluster, we use the upstream directive

http {
   upstream local{server 10.10.11.11:8088; Server 10.10.11.12:80; } location / { 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://local; }}Copy the code

The proxy_pass command configures forwarding to local and searches for upstream local server, where the default weight of the server IP list is 1, in round-robin mode

The proxy_set_header directive does the following:

Because of the proxy server, the upstream service does not actually get the header data from the client. It can use proxy_set_header to return the header data

Use proxy_cache directives to speed up performance (back source -nginx as cache server)

We, nginx, act as a reverse proxy and forward each request to the back-end service. But for scenarios where data requirements are not strictly consistent and traffic is high, we can use the proxy_cache directive in the Nginx proxy layer to increase the cache and speed up our response time.

Declaring the Cache Configuration

proxy_cache_path /usr/local/nginx/nginxcache/test keys_zone=cache_test:10m levels=1:2 inactive=12d max_size=200m;
Copy the code

Proxy_cache_path Cache file path

Levels Sets the level of the cache file directory. Levels =1:2 indicates two levels of directories

Keys_zone Sets the cache name and shared memory size

Inactive is deleted if no one accesses it within a specified period of time

Max_size Maximum cache space. If the cache space is full, the resources with the longest cache time are overwritten by default.

The cache proxy_cache directive is configured

location / {

          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:/xx.xx.xx.xx;

         # cache configuration
         proxy_cache cache_test; 
         # cache key
         proxy_cache_key $host$uri;  
         Cache status code Cache duration. Change to any to match all status codes
         proxy_cache_valid 200 304 302 1d;
   }

Copy the code

The proxy_cache directive is affected by the browser cache headers, so if we want to force the cache, we need to configure proxy_ignore_headers as follows:

 proxy_ignore_headers Expires Cache-Control;
 proxy_cache cache_test;
 proxy_cache_key $host$uri;
 proxy_cache_valid 200 304 302 1d;
Copy the code

More exciting content to pay attention to the public number: dull bear a point