Common usage of nginx

They sell fried dough sticks. 2021-02-23 04:05:09
common usage nginx


It's going to be right here nginx Common usage to do a basic introduction , For others, please refer to nginx file .

summary

ngnix It's a server , Can be used to do

  • http The server
  • http Reverse proxy
  • mail proxy server
  • tcp/udp proxy server

Basic usage

It is assumed that download End .
nginx There are main processes and some work processes , The main process is responsible for reading and calculating the configuration and maintaining the work process , The worker process processes the real request , Profile defaults to /etc/nginx/nginx.conf

start-up 、 Stop and restart

Use the following syntax for related operations

nginx -s signal
 Copy code 

among signal Take

  • stop Force it to stop immediately
  • quit Stop but wait for the current request to be processed
  • reload restart
  • reopen Reopen logging , It can be used after the old log files are removed , Regenerate the log file

Configuration file structure

nginx Contains many modules , We can use the instructions in the configuration file to control .
A simple instruction contains spaces separated by name And parameters , Add a semicolon at the end .
A block instruction (block directive) Similar to a simple instruction , But it doesn't end with a semicolon, it ends with a curly bracket , If there are other instructions in curly braces , Is called a context (context), such as events, http, server, and location. Any place outside the context is considered the main context (main context), among events and http The instruction is in the main context ,server stay http Context ,location stay server Context .

The content after the well number is considered to be a note .

Static resource services

web Servers can be used to provide static resource access , Different paths can be accessed on request , For example http Context add the following configuration

server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
 Copy code 

location To match the parameters of a block instruction , If a request can match multiple paths, the longest one will prevail .
For example, request / Will match the first , Go to /data/www/ Access to resources , request /images/ It matches the second one , To /data/images/ Get resources .

proxy server

A proxy server is a server , When a request is received , Will send the request to the proxy server , And then send the request to the client .

Let's make a simple proxy server here , receive 80 Port requests , The pictures use the local directory , Other requests are sent to 8080 port .

Use two... In the configuration file server Block instruction , The first monitor 80 port , For images, we use regular check related suffixes , We need to use ~.

server {
location / {
proxy_pass http://localhost:8080;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
 Copy code 

Another monitor 8080 port , ad locum server The context contains root Field , When location Not in context root This is used by default when setting the parameter .

server {
listen 8080;
root /data/up1;
location / {
}
}
 Copy code 

nginx How to deal with a request

be based on name Virtual server for

We can configure three monitors 80 Port of the virtual server

server {
listen 80;
server_name example.org www.example.org;
...
}
server {
listen 80;
server_name example.net www.example.net;
...
}
server {
listen 80;
server_name example.com www.example.com;
...
}
 Copy code 

This configuration will respond to requests header Medium host Field for routing , If it doesn't match , It will be routed to the default server , The default server is the first , You can also use parameters default_server Appoint

server {
listen 80 default_server;
server_name example.net www.example.net;
...
}
 Copy code 

If not, No host Request access to fields , You can use the following configuration to close the connection , Where the null character is name It can be matched

server {
listen 80;
server_name "";
return 444;
}
 Copy code 

there server_name It can be expressed in the following form

  • accurate name
  • Start with an asterisk , such as *.example.org
  • It ends with an asterisk , such as mail.*
  • Regular

Use http Load balancing

Load balancing across multiple application instances is often used to optimize resource utilization 、 Maximize throughput 、 Reduce latency and fault tolerance .
We can use nginx As an effective load balancer to distribute traffic to multiple application servers , To improve performance 、 Stability and reliability .

nginx There are three ways of load balancing

  • round-robin Assign requests in turn
  • least-connected Assign the request to the least ordinal active connection
  • be based on client Of ip Corresponding hash Value to assign , This ensures that the same request is processed by the same server

The simplest configuration is

http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
 Copy code 

Default round-robin, If you want to use least-connected or ip_hash Add instructions to the server group

 upstream myapp1 {
least_conn;# or ip_hash
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
 Copy code 

To configure https

Need to add ssl Parameters (http2 Parameters can be turned on http2), And specify the relevant certificate and private key

server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
...
}
 Copy code 

In order to http Request forwarded to https therefore , To put 80 The data of the port is forwarded to https

 server {
listen 80;
rewrite ^(.*)$ https://$host$1
}
 Copy code 

add to header

Can be in http, server, location Context use add_header Add response header, grammar

add_header name value [always];
 Copy code 

such as

 location / {
add_header Cache-Control 0;
}
 Copy code 

The end

版权声明
本文为[They sell fried dough sticks.]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210223024104363p.html

  1. JavaScript advanced: Javascript object-oriented, JavaScript built-in object, JavaScript BOM, JavaScript encapsulation
  2. JavaScript advanced: Javascript object-oriented, JavaScript built-in object, JavaScript BOM, JavaScript encapsulation
  3. Vue determines whether the El form in the elementui is updated or changed. If it changes, it will prompt whether to save it. If it does not change, it will leave directly
  4. Algorithm problem: sum of two numbers -- JavaScript and Java implementation
  5. High performance nginx HTTPS tuning
  6. JQuery advanced
  7. day 30 jQuery
  8. JQuery:JQuery Basic syntax, jQuery selector, jQuery DOM, comprehensive case check box, comprehensive case random picture
  9. TCP/IP 开胃菜 之 HTTP
  10. JQuery:JQuery Basic syntax, jQuery selector, jQuery DOM, comprehensive case check box, comprehensive case random picture
  11. JavaScript data type
  12. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  13. Solve Ajax cross domain problem [5 solutions]
  14. HTTP of TCP / IP appetizer
  15. Optimization of pod creation efficiency in serverless scenario
  16. Iqiyi Sports: experience the ultimate expansion and contraction of serverless, and increase the utilization rate of resources by 40%
  17. First knowledge of HTTP / 1.1
  18. First knowledge of HTTP / 1.1
  19. Webpack learning notes series 05 devserver
  20. Webpack learning notes series 04 - resource processing optimization
  21. How to build a high performance front end intelligent reasoning engine
  22. How to become a professional front end engineer in 2021?
  23. How to transform single / micro service application into serverless application
  24. How to transform single / micro service application into serverless application
  25. How to transform single / micro service application into serverless application
  26. How to connect the ground gas to the micro front end?
  27. How to connect the ground gas to the micro front end?
  28. How to connect the ground gas to the micro front end?
  29. Vue server rendering principle analysis and introduction
  30. Realize the correct loading of text message
  31. Building my own project scaffolding with yeoman
  32. JavaScript advanced prototype and prototype chain
  33. React background management front end system (based on open source framework development) start
  34. JS practical skills breakpoint debugging
  35. I'd like to share with you 20 super easy-to-use Chrome extension plug-ins
  36. Get page element location
  37. Use the powerful API of modern browser to record any interface in the browser and realize export, save and management
  38. Delayed code execution in flutter
  39. Reconfiguration experience of KOA middleware system
  40. Add comments to your blog
  41. Svg editor -- new path
  42. Detailed explanation of debounce and throttle of JavaScript function
  43. Anti shake and throttling and corresponding react hooks package
  44. C2m: the first CSDN article moved to MOOC script 5000 words, detailed painstaking development process, there are renderings and source code at the end of the article
  45. Front end, school recruitment, Taobao, guide
  46. [vue2 & G6] get started quickly
  47. Canvas from the beginning to the pig
  48. Take five minutes to standardize the code comments?
  49. Some thoughts on sass
  50. what?! You haven't filled in the award information yet
  51. How to get the interface + tsdoc needed by TS through swagger
  52. Binary tree
  53. Canvas drawing method in Web screenshot
  54. Front end docker image volume optimization (node + nginx / node + multi-stage construction)
  55. Become a big influence of technology? Coding pages quickly build personal blog
  56. Object and array deconstruction, spread operator, rest operator
  57. Analysis of Axios source code
  58. Two ways to delete useless code in project (Practical)
  59. Edit your picture with canvas
  60. Today's chat: 2-4 years to walk out of the resignation dilemma and comfort zone