Nginx configuration file
Why have we been focusing on nginx? It is because it is a very powerful server. If Lua plug-in language is used together, it will be a new popular technology in the next few years, because it is too light, the architecture between servers is the link, modification, and addition of the configuration file. The following describes how to configure nginx:
Nginx Configuration
The Nginx configuration mainly involves modifying the/usr/local/nginx/conf/nginx. conf file.
Configure users and user groups
user www www;
Number of worker processes. We recommend that you set it to the total number of CPU cores.
worker_processes 2;
The global Error Log definition type. The log levels are as follows:
debug | info | notice | warn | error | crit
error_log logs/error.log info;
File that records the master process ID
pid /usr/local/nginx/nginx.pid;
** The maximum number of file descriptors that a process can open. In theory, this value is calculated based on the maximum number of files that can be opened by the number of processes. However, nginx load is not fully balanced,
Therefore, this value should be equal to the maximum number of files that can be opened. Run sysctl-a | grep fs. file to view the linux file descriptor. **
worker_rlimit_nofile 65535;
Working mode and maximum number of connections
Events {# working mode, epoll use epoll later than linux2.6; # maximum number of connections allowed by a single process worker_connections 65535 ;}
Sets the http server and uses its reverse proxy function to provide Load Balancing support
Http {
# File extension and file type ing table include mime. types; # default file type default_type application/octet-stream; # log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
# Access log records users, pages, user browsers, ip addresses, and other access information access_log logs/access. log main; # server name hash table size server_names_hash_bucket_size 128; # client request header buffer size. By default, nginx uses the buffer client_header_buffer_size to read the header value. # If the header is too large, it uses large_client_header_buffers to read it. # If the HTTP header is Too small or the Cookie is Too large, the system will report the 400 Error nginx 400 bad request # If the buffer is exceeded, the system will report the HTTP 414 error (URI Too Long) # The maximum size of the HTTP header accepted by nginx must be larger than one of its buffer; otherwise, a 400 HTTP Error (Bad Request) will be reported ). Client_header_buffer_size 32 k; Listen 4 32 k; # client request body size client_body_buffer_size 8 m; # Hide ngnix version server_tokens off; # ignore invalid request header ignore_invalid_headers on; # specify to enable error_page other than the first error_page command.
recursive_error_pages on;
# Enable nginx not to use the first domain name in server_name settings by default when handling internal redirection.
server_name_in_redirect off;
# Enable file transfer. Generally, the application should be set to on. If a download application exists, you can set it to off to balance network I/O and disk I/O to reduce the system load sendfile on; # Tell nginx to send all header files in a data packet, instead of sending one by one. Tcp_nopush on; # Tell nginx not to cache data, but to send data in a segment. When data needs to be sent in a timely manner, set this attribute for the application, # The returned value cannot be obtained immediately when a small piece of data is sent. Tcp_nodelay on; # long connection timeout time, in the unit of seconds keepalive_timeout 65; # gzip module setting, gzip compression can reduce the bandwidth consumption of the website and increase the access speed. Gzip on; # enable gzipgzip_min_length 1 k; # minimum compression size gzip_buffers 4 16 k; # compression buffer gzip_http_version 1.0; # compression version gzip_comp_level 2; # compression grade gzip_types text/plain application/x-javascript text/css application/xml; # compression type
Upstream is used for load balancing. Configure the server address and port number to be polling. max_fails is the number of failed requests allowed. The default value is 1.
# Weight is the round robin weight, which can be used to balance the server's renewal rate based on different weights. Upstream ligh {server 192.168.2.149: 8080 max_fails = 0 weight = 1; server 192.168.1.9: 8080 max_fails = 0 weight = 1 ;}
# Host configuration server {
# Listening port
Listen 80; # domain name server_name ligh;
# Character Set charset UTF-8; # Separate access_log file access_log logs/192.168.2.149.access.log main; # reverse proxy configuration, set all requests to http: // all requests for hostname are forwarded to the target server defined in upstream. Location/{# The domain name configured here must be the same as the upstream domain name before forwarding. Proxy_pass http: // ligh; proxy_set_header X-Real-IP $ remote_addr;} # enable the nginx status listening page location/nginxstatus {stub_status on; access_log on ;} # error page error_page 500 502 503 504/50 x.html; location =/50x.html {root html ;}}
}
Now, the basic load balancing configuration of nginx is complete. In this experiment, two tomcat servers are deployed, and different results are returned during access. Enter the address in the browser, that is, the nginx service access address is OK.