Nginx configuration file details

Main article has been copied: https: //blog.csdn.net/houkai18792669930/article/details/93387477
supplement article: https: //blog.csdn.net/wangbin_0729/article/details/82109693


#nginx configuration file is divided into six areas:

main (Global Settings)

events (nginx operating mode)

http (http settings)

Sever (Main Settings)

location (URL match)

upstream (load balancing server settings)

################### main area ############################# ####
#user: to specify Nginx Worker processes running users and user groups, the default is run by nobody account. You can also create a user-specified user nginx.

Www user to create, in nginx configuration file in the user noboby noboby; -> user www www; you can

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

#worker_processes: to specify the number of child processes Nginx to open. Each Nginx process takes an average of 10M ~ 12M memory. According to experience, generally specify a process is sufficient, if it is a multi-core CPU,

As the number of CPU and recommendations specified number of processes can be. I am here to write 2, it will open two sub-process, a total of three processes.

#error_log: used to define the global error log file. Log output levels are debug, info, notice, warn, error, crit to choose from, debug output log the most the most detailed, and crit output minimal logging.
#pid: process id used to specify the location of the file.
#worker_rlimit_nofile: used to specify the maximum number of open file descriptors a process can nginx, here is 65535, use the command "ulimit -n 65535" is set.

user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;

##################### event area ########################### ####
#use: Nginx is used to specify the mode of operation. Nginx supports operating mode have select, poll, kqueue, epoll, rtsig and / dev / poll.

Which select and poll are standard operating mode, kqueue epoll and efficient mode of operation, different epoll is used in the Linux platform,

And kqueue used in BSD systems, for Linux systems, epoll is the preferred mode of operation.

#worker_connections: Nginx maximum number of connections defined for each process, i.e., the front end of the maximum number of requests received, the default is 1024.

The maximum number of connections determined by the client and worker_processes worker_connections, i.e. Max_clients = worker_processes * worker_connections,

When as a reverse proxy, Max_clients becomes: Max_clients = worker_processes * worker_connections / 4.

Maximum number of connections by the process of Linux systems process the maximum number of open file limit, to take effect in the "ulimit -n 65536" after the worker_connections execute operating system commands set.

events {
use epoll;
worker_connections 1024;
}
######################### http设置#####################################

http HTTP server module responsible for configuring the relevant attributes, there are two sub-modules server and upstream

http {

#include: to mime.type file defines the set with a mime type of the file, type in the profile directory, to tell nginx to identify the file type.
#default_type: set the default type as a binary stream, that is, use this approach when the file type is not defined, for example, in the absence of the asp locate the configuration environment, Nginx is not resolved, this time, to access the browser asp file will be downloaded.
#log_format: format used to set the log, and which parameters record, set here to main, just for the record access_log to this type.
the mime.types the include;
default_type file application / OCTET-Stream;
log_format main '$ REMOTE_ADDR - r e m O t e in s e r [ remote_user [ time_local] “KaTeX parse error: Double superscript at position 34: … '̲status b O d Y b Y t e s s e n t " body_bytes_sent " http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " http_x_forwarded_for”’;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;

######################### server settings ####################### ##############
#server for a given virtual host, marking the beginning of the definition of virtual hosts.
#listen: used to specify the virtual host service port.
#server_name: to specify the IP address or domain name, with a space between a plurality of domain names.
#root: expressed in this entire server web hosting, all the root web root directory. Note To locate {} and distinguish defined below.
#index: default home page address is defined globally accessible. Note To locate {} and distinguish defined below.
#charset: The default encoding format settings pages.
#access_log: used to specify the access log storage path for this virtual host, the last main output format for specifying an access log.
{Server
the listen 80;
server_name localhost;
the root / the Users / HK / WWW;
index the index.php index.html index.htm;
charset UTF-. 8;
access_log logs / main host.access.log;
aerror_log logs / host.error.log main;

######################### location settings ####################### ##############

location module load balancing, reverse proxy, virtual domain configurations. It is to be located and URL, parse URL, it also provides a powerful regular matching function, and also supports conditional match,

For static and dynamic web pages filtering process may be implemented by Nginx instruction location.

# / Means match access the root directory.
When #root instruction for designating access the root directory, virtual host web directory, the directory may be a relative path (relative path is relative to the install directory nginx). Or an absolute path.
#proxy_pass: forwarding agent, if added proxy_pass behind url /, represents the absolute root path; Without /, represents a relative path, the path of the matching portion is also go to the proxy.
#proxy_set_header: Allow redefine or addition request header sent to the backend server.
#include: loading a configuration file, described later when nginx multiple profiles will be mentioned.
#root: url path to locate resources localtion match.
#index: define the page display html, general, and with the use of alias.
LOCATION / {
the root HTML;
index index.html index.htm;
}

    error_page  404              /404.html;       
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    
    
    #反向代理配置
    location /jyb {
        proxy_pass http://qurt/;
        proxy_read_timeout 1800s;
        proxy_set_header   Host $host:$server_port;
        proxy_set_header   X-real-ip  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_set_header   X-Forwarded-Proto  $scheme; 
     }
 
    
     #采用uwsgi方式
     location /python/ {
         include uwsgi_params;
         uwsgi_pass 127.0.0.1:33333;
     }
    
    # FastCGI方式
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    #访问nginx本机目录的文件
    location / {
        root   /home/hk/;
        index  index.html index.htm;
    }
    
    location  /static/ {
         alias /var/static/;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}


# another virtual host using mix of IP-, name-, and port-based configuration
server {
    listen       8000;
    listen       somename:8080;
    server_name  somename  alias  another.alias;

    location / {
        root   html;
        index  index.html index.htm;
    }
}


# HTTPS server    
server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   html;
        index  index.html index.htm;
    }
}

############## upstram module ################

upstream module debt load balancing module, through a simple scheduling algorithm is implemented to load balance client IP backend server.

#Nginx load balancing module currently supports four scheduling algorithms:

weight polling (default). Each request individually assigned to a different time order back-end server, if a back-end server is down, the system failure is automatically removed, so that the user access is not affected.

Polling a weight higher weights, the greater the weight value assigned to the access probability variation is mainly used for the case where the rear of each server performance.

ip_hash. Each request is assigned by hash result Access IP, so that visitors from a fixed IP to access the same back-end server, an effective solution to the problem of sharing session dynamic pages.

fair. More than the above two intelligent load balancing algorithm. Such a load balancing algorithm can be based on the page size and load duration intelligently,

I.e. the allocation request according to the response time of the back-end server, a short response time priority allocation. Nginx does not support the fair itself, if need to use this scheduling algorithm, you must download the Nginx upstream_fair module.

url_hash. Press access url hash result to the allocation request, each url directed to the same back-end server, efficiency can be further improved backside cache servers. Nginx is not itself support the url_hash,

If you need to use this scheduling algorithm, Nginx the hash package must be installed.

# In HTTP Upstream module can be specified by the back-end server command server IP address and port, but also can set the status of each back-end server load balancing scheduling. Commonly used state are:

down, it indicates the current server time being does not participate in load balancing.

backup, backup machine reserved. When all other non-backup machine failure or busy, does not request backup machine, so the pressure of the lightest machine.

max_fails, allowing the number of failed requests, the default is one. When the maximum number of attempts to return proxy_next_upstream module defined error.

fail_timeout, after a max_fails failures, pause time services. max_fails can be used with fail_timeout.

# Note that when the load scheduling algorithm ip_hash, the back-end server load balancing scheduling state can not be a weight and backup.
# Note: nginx's worker_rlimit_nofile reaches the upper limit, then the client has reported 502 errors after linking with the log_format directive sets the log format, you need to specify the log file storage path with access_log directive.

upstream server_group {
    ip_hash;
    server 192.168.123.1:80;
    server 192.168.123.2:80 down;
    server 192.168.123.3:8080  max_fails=3  fail_timeout=20s;
    server 192.168.123.4:8080;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://server_group/;
    }
}

}

###################### nginx the location and alias in the root of the difference between ################### #
nginx specify a file path in two ways root and alias, both usage differences, use summarized.
The main difference is the root alias explain how nginx location behind uri, which makes the two different ways, respectively map the request to the server files.
[root]
syntax: root path
defaults: root html
configuration section: http, server, location, if

[alias]
    语法:alias path
    配置段:location

root实例:

    location ^~ /t/ {
        root /www/root/html/;
    }
    如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。

alias实例:
    location ^~ /t/ {
        alias /www/root/html/new_t/;
    }
    如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,
    因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
注意:
    1. 使用alias时,目录名后面一定要加"/"。
    2. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
    3. alias只能位于location块中。(root可以不放在location中)
Published 212 original articles · won praise 151 · views 500 000 +

Guess you like

Origin blog.csdn.net/yhj19920417/article/details/50102043