The installation

  • Windows:www.cnblogs.com/jiangwangxi…
  • Centos:segmentfault.com/a/119000001…

Some basic commands

  • Start thestart nginx
  • Shut downNginx -s stop(quick stop nginx) or nginx -s quit(complete orderly stop nginx)
  • Viewing the Running resulttasklist /fi "imagename eq nginx.exe"(Windows)
  • Reload the configuration filenginx -s reload

About the configuration

Reference: nginx configuration file example

The server configuration

server { listen 80; server_name localhost; location / { root html; index index.html index.htm; }}Copy the code

If you use localhost to make a direct request, the first thing you’ll get is a “/”, and then the root and index directives will look for index.html first, or index.htm if not

Proxy interface forwarding

server { listen 80; Server_name 127.0.0.1 location / {proxy_pass HTTP://auth.nie.netease.com/
    }
    
    location /shop/ {
        proxy_pass http://auth.nie.netease.com/}}Copy the code

When directly access 127.0.0.1 default access “/”, then according to the configuration of proxy_pass forwarded to http://auth.nie.netease.com/, If access to 127.0.0.1 / shop/will according to the configuration of proxy_pass forwarded to http://auth.nie.netease.com/

When configuring proxy_pass proxy forwarding in nginx, if/is added to the URL after proxy_pass, it indicates the absolute root path. If there is no /, it indicates the relative path, and the matched part of the path is also passed to the agent.

Location /proxy/ {proxy_pass HTTP:/ / 127.0.0.1;} proxy to URL: HTTP:/ / 127.0.0.1Location /proxy/ {proxy_pass HTTP:/ / 127.0.0.1;} proxy to URL: HTTP:/ / 127.0.0.1 / proxy
Copy the code

The cache

Caching can be implemented using the HTTP_proxy module of Nginx. When caching is enabled, Nginx stores the data in the disk cache and uses the cached data in response to client requests as long as the cached data has not expired

Nginx configures caching

  • Add_header: you can configure the cache policy for HTTP headers and the validity period of resources
Syntax: add_header name value Default value: none Fields: HTTP, server, location add_header cache-control'private, max-age=0 no-cache';
Copy the code
  • Expires: This directive controls whether to mark an expiration time in the reply
Grammar: expires [time | epoch | | Max off]. Default value: Expires off. Use fields: HTTP, Server, location. Off disallows changes to header Expires and cache-control. Time Specifies the value of cache-control. A negative value indicates no-cache. Epoch sets the Expires header to 1 January, 1970 00:00:01 GMT. Max sets the Expires header to 31 December 2037 23:59:59 GMT, maximizing cache-control to 10 years. location ~* \.(jpg|jpeg|gif|bmp|png){ expires 1d; # cache for 1 day}Copy the code

When the image is first accessed, the max-age of cache-control is 86400, which is the number of seconds in a day