The following configuration is required when using Nginx

location

Location matches path for distribution

Location/XXX match path location = / XXX exact match/XXX location ~ ^/ ABC $must match with the re, where ~ indicates that this is a re, ^ and $are the beginning and end of a regular expression meaning location! Location ~* ^/ ABC $~* indicates a regular and case-insensitive location ^~ / XXX If this match is successful, no further matches will be performedCopy the code

The sample

location / { } location /test { } location ~ ^/test/([0-9]*)/? ${/test/123} ${/test/123}Copy the code

rewrite

Rewrites any path to the specified path without changing the URL

location / { rewrite /test/(.*)+/? /index.html? s=$1; }Copy the code

In the example above, if the /test/ XXX directory did not exist on the server, accessing /test/ XXX would have returned 404, but because of the rewrite rule added, it actually accessed /index.html? /test/ XXX = /test/ XXX

/test/ XXX: /test/ XXX: /test/ XXX: /test/ XXX: /test/ XXX: /test/ XXX: /test/ XXX: /test/ XXX

if ( ! -d $request_filename) {$request_filename = $request_filename; } if ( ! -f $request_filename) {# set $rf to 21 set $rf 2$rf; Rewrite /test/(.*)+/? /index.html? id=$1; }Copy the code

try_files

Try to open the file specified by path. If the file does not exist, proceed to the next file. If neither file exists, 500 is returned

location / {
  try_files $uri $uri/ /index.html;
}
Copy the code

If /test/123 does not exist, try to open /test/123/. If /test/123/ does not exist, open /index.html. In index.html file, you can obtain the URL through JS to obtain /test/123, and then realize the corresponding route by yourself

Aliases can also be used

location / { try_files $uri $uri/ @test; } location@test {# do something about it}Copy the code

Load balancing

Define the upstream service first

Upstream API {# add multiple backend servers and specify health check server 192.168.0.28:8088; keepalive 300; keepalive_requests 1000; keepalive_timeout 60s; }Copy the code
server { listen 80; server_name ahydd.com; root /usr/share/nginx/html/; location /api { proxy_pass http://api; Proxy_http_version 1.1; proxy_set_header Connection ""; }}Copy the code

HTTP jump HTTPS

server {
  listen 80;
  server_name ahydd.com;
  return 301 https://$server_name$request_uri;
}
Copy the code

Disallow certain request modes

Prohibit the GET | POST | PUT | DELETE | OPTIONS outside of the request

location / {
  if ($request_method !~ ^(GET|POST|PUT|DELETE|OPTIONS)$ ) {
    return 403;
  }
}
Copy the code

Common starting Settings

user root; worker_processes auto; worker_rlimit_nofile 65535; events { worker_connections 102400; use epoll; } http { server_tokens off; include mime.types; default_type application/octet-stream; #access_log /var/log/nginx/access.log; access_log off; error_log /var/log/nginx/error.log; keepalive_timeout 65; client_max_body_size 20m; gzip on; gzip_disable "msie6"; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml application/javascript text/css application/x-javascript; Upstream API {server 192.168.0.28:8088; keepalive 300; keepalive_requests 1000; keepalive_timeout 60s; } server { listen 80; server_name empty; } server { listen 80; server_name ahydd.com; root /usr/share/nginx/html/; location / { try_files $uri $uri/ /index.html; } location /api { proxy_pass http://api; Proxy_http_version 1.1; proxy_set_header Connection ""; }}}Copy the code

Public number: fly code