# Example of nginx.conf file
user nobody;
worker_processes 8;
error_log varlog/nginx/error.log error;
#pid logs/nginx.pid
events {
use epoll;
worker_connections 50000;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr [$time_local] "$request" ‘;
access_log logs/access.log main buffer=32k;
...
}
All event class configurations are required In events
events {…
}
http {
upstream backend {
server 127.0.0.1:8080;
}
gzip on;
< p> server {
…
location /webstatic {< /p>
gzip off;
}
}
}
block configuration Items can be nested. The inner block directly inherits the outer block. When a conflict occurs, the inner layer shall prevail. For example, the gzip above is on for the outer layer, and gzip for the inner layer of the location is off.
The block configuration item consists of a block configuration item name and a pair of braces
Basic The syntax format of the configuration item is:
Configuration item name Configuration item value 1 Configuration item value 2 …;
At the beginning of the line is the configuration name, and these configuration names must be nginx One of the modules needs to be processed, otherwise nginx will consider the illegal configuration name in the configuration file.
After the configuration item name ends, use a space as a separator
The configuration item value can be a number. Strings and regular expressions can have only one value or multiple values. A semicolon must be added to the end of each line of configuration.
If the configuration item value includes syntax symbols such as spaces, single/double quotation marks are required Enclose the configuration item value
nginx.conf The comments start with #
The unit of the configuration item
Can be used when specifying the space size: K /k Means KB M/m Means MB
The time unit can be Use: ms (milliseconds) s (seconds) m (minutes) h (hours) d (days) w (weeks, 7 days) M (months, 30 days) y (years, 365 days)
Configuration items Use variables in the (only some modules support this way of using variables)
log_format main’$remote_addr- $remote_user [$time_local] “$request” ‘;
In the above example, $remote_addr represents a variable remote_addr
Basic configuration of nginx service
daemon on|off; The default daemon on; Set whether nginx runs as a daemon (a process running away from the terminal in the background)
master_process on|off; The default master_process on; nginx in the production environment runs in a way that one master process manages multiple worker processes;< /span>
If it is closed, the worker child process will not be fork, but the master process itself will handle the request
debug_connection[IP|CIDR] Changing the configuration is an event The configuration must be placed in events{…} to be effective, which can be an ip address or a cidr address. Make sure that the –with-debug parameter is used when compiling and executing congfigure.
such as
events {
debug_connection 10.224.66.14;
debug_connection 10.224.57.0/24;
}
This will only come from the above IP address The debug level log will be output only for the request, other requests still use the log level configured in error_log
worker_rlimit_core size; limit the size of core dump core dump file
working_directory path; The working directory of the worker process is used to set the storage directory of the coredump file, and the worke r process has permission to write files to the directory specified by working_directory
include pathfile; Import the configuration in other configuration files, pathfile can be an absolute path or a relative path (relative to the directory where nginx.conf is located) such as
include mime.types;
include vhost/*.conf;
The parameter value can be a clear file name, or a file name with wildcard *, and multiple configuration files can be embedded at one time
pid pathfile; pid pid default logs/nginx.pid; It is necessary to ensure that nginx has the right to create pid files in the corresponding target
user username[ groupname]; Default user nobody nobody; Set the work of fork after the master process is started Which user and user group the er process runs under. When the user group is not set, the user group is the same as the user name
worker_processes number; The default worker_processes 1; Under the master/worker operation mode, define the number of worker processes
use [kqueue|rtsig|epoll|/dev/poll|select|poll|eventport]; By default nginx will automatically use the most suitable event model
worker_connections number; define each worker process The maximum number of connections that can be processed at the same time
Static web server
A typical static web server will also contain multiple server blocks and location blocks span>
http {
gzip on;
upstream {
...
}
...
server {
listen localhost:80;
...
location /webstatic {
if ...
{
...
}
root opt/web/resource;
...
}
location ~* .(jpg|jpeg|png|jpe|gif)$ {
...
}
}
server {
...
}
}
All http configuration items must directly belong to http block, server block, location block, upstream block or if block, etc.
< span style="font-family:'comic sans ms', sans-serif; font-size: 15px;">Some configuration items can appear in a certain block arbitrarily, and some configuration items can only appear in a specific block .
The static web server provides many functions: virtual host and Request distribution, file path definition, allocation of memory and disk resources, network connection settings, MIME type settings, restrictions on client requests, optimization of file operations, and special handling of client requests.
Virtual Host and Requested Distribution
usually there are multiple host domain names corresponding to the same IP address , Each server block is a virtual machine, it only processes the corresponding host domain name request
In this way, nginx on a server can process http requests to access different host domain names in different ways.
server{
listen address:port
}
the default is listen 80; the listen parameter determines how nginx is Listen port, you can add only IP address, port or host name after listen
Example:
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000; p>
If it is an IPV6 address, it can be written as
listen [::] :8000;
listen [fe80::1];
listen [:::a8c9:1234]:80;
You can add others after the address and port Parameters, such as:
listen 43 default_serer ssl;
listen 127.0.0.1 default_server accept_filter=dataready backlog=1024;
default/default_server: Set the server block as the default server block of the entire web service. If it is not set, it will be the first A server block is the default server block.
cannot The request that matches any host domain name in the configuration file will be processed by the default server
# nginx.conf file example
user nobody;
worker_processes 8;
error_log varlog/nginx/error.log error;
#pid logs/nginx.pid
events {
use epoll;
worker_connections 50000;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr [$time_local] "$request" ‘;
access_log logs/access.log main buffer=32k;
...
}
http {
gzip on;
upstream {
...
}
...
server {
listen localhost:80;
...
location /webstatic {
if ...
{
...
}
root opt/web/resource;
...
}
location ~* .(jpg|jpeg|png|jpe|gif)$ {
...
}
}
server {
...
}
}