Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Nginx configuration File

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of the Nginx configuration file, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Overview

Nginx configuration file, as well as some configuration recommendations

2. Nginx.conf1) configuration file location

Under the conf folder of the nginx installation directory, for example: / usr/local/nginx/conf/nginx.conf.

You can also place the configuration file anywhere and specify the location of the configuration file when you start Nginx, for example:. / nginx-c / home/nginx.conf

2) worker_processes

Set the number of worker, Nginx's process model uses master, worker mode, one master is responsible for coordination, multiple worker is responsible for interaction with the client.

Set it to auto here.

3) events

Set the model used and the number of connections per worker.

Epoll is recommended for models in the Linux operating system.

The number of connections to worker is usually set to 10240, but it can also be increased appropriately if the hardware resources are very good.

4) include

Contains the contents of another file and places the contents of another file at the tag.

Multiple files can be written to multiple include, or wildcards can be used to match multiple files.

5) sendfile and tcp_nopush

Sendfile: sets whether efficient file transfer is enabled. It is enabled by default.

Tcp_nopush: it is only valid when the sendfile value is on. When tcp_nopush is set to on, it will be sent when the packet accumulates to a certain size, which helps to improve the efficiency of file transfer.

It is recommended that all be set to on.

6) keepalive_timeout

The timeout for the client to connect to the server, before the connection is disconnected, the client interacts with the server again, and the connection can be reused without the need to re-establish a connection, reducing the cost of resources.

Set to 0, it disconnects immediately after the interaction. This value can be kept by default.

7) gzip

Set to on, which means that the data will be compressed before transmission, which will increase the efficiency of transmission and save bandwidth, but will affect the performance of the server CPU.

To turn on this configuration, you need to configure some additional properties.

You can weigh whether to save bandwidth or improve CPU performance. It is recommended to enable it and configure it according to the actual situation.

The code is as follows:

Gzip on;gzip_min_length 512; # limits the minimum compression in bytes, less than this value will not compress gzip_comp_level 5; # the level of compression, the value is 1 to 9, the higher the level, the greater the proportion of compression, the more cpu consumption, the more gzip_types text/plain application/javascript text/css image/jpeg image/gif image/png application/json; # the file types to be compressed

8) server

A server block is a virtual service.

You can specify the port, service name, routing rules, and other information of the virtual service in the server block.

There can be multiple server.

Under one server, there can be more than one location.

Server {listen 90; # port server_name localhost; # service name, which can be IP address or domain name. When the port is the same, which routing rule location / {# root path routing rule root html will be chosen according to the service name # corresponding to the html folder under the nginx installation target, it can also be set to an absolute path, for example: root / home/html; index hello.html; # specifies that the default home page is hello.html} location / hello {root / home/hello # index omitted, which means there is no default page} error_page 500502 503504 / 50x.hml; # error page location = / 50x.html {root html;}} 9) detailed description of the matching rules of location

Server blocks contain location blocks, and location blocks can have multiple under one server, mainly to configure the routing rules for requests.

Nginx matches the location block according to the requested resource path, and then forwards the route according to the configuration of location.

Location supports a variety of matching rules, so let's talk about it in more detail.

Exact matching

Location / {# Root path routing rule root html; # corresponds to the html folder under the nginx installation target, or can be set to an absolute path, for example: root / home/html; index hello.html; # specifies that the default home page is hello.html} location / hello {root / home/hello # index omitted, which means no default page}

Regular expression matching

Location ~ *\. (GIF | PNG | jpg | bmp | jpeg) {# * represents case-insensitive root / home/img;}

Match requests that start with a path

Location ^ / server/page/ {root / home/page;} above is all the contents of the article "sample Analysis of Nginx configuration Files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report