Current location - Quotes Website - Team slogan - How to configure Nginx to be highly available
How to configure Nginx to be highly available
Nginx configuration

The configuration of Nginx is mainly to modify the /usr/local/nginx/conf/nginx, conf file.

# Configure users and user groups

User www www

# number of worker processes, it is recommended to set it to the total number of CPU cores.

Work process 2;

# The global error log defines the type, and the log level from low to high is: debug | info | notice | warn | error | crit.

Error_log log /error.log information;

# File that records the ID of the main process

PID/usr/local/nginx/nginx . PID;

# The maximum file descriptor that a process can open. Theoretically, this value should be the maximum number of files that can be opened divided by the number of processes. But because nginx load is not completely balanced,

# So this value is better equal to the maximum number of files that can be opened. Execute sysctl -a | grep fs.file to view the linux file descriptor.

worker _ rlimit _ nofile 65535

# Working mode and maximum number of connections

Event {

# working mode, epoll is used for linux2.6 version 2.6 and above.

Use epoll

# Maximum number of connections allowed by a single process

Worker _ Connection 65535;

}

# Set p _ level2; # Compression level

Gzip_types text/general application /x-javascript text /css application/XML; # Compression type

#upstream is used for load balancing. Configure the address and port number of the server to be polled here. Max _ failures is the number of allowed request failures, and the default value is 1.

#weight is the polling weight, and the access rate of the server can be balanced according to different weights.

Upstream hostname {

Server192.168.2.149: 8080max _ failures = 0 weight =1;

Server192.168.1.9: 8080max _ failures = 0 weight =1;

}

# Host Configuration

Server {

# Listening port

Listen to 80;

# domain name

Server Name Hostname;

# Character set

Character set utf-8;

# Separate access log file

access _ log logs/ 192. 168.2 . 149 . access . log main;

# Reverse the proxy configuration and forward all requests with http://hostname to the target server defined in the upstream.

Location/{

# The domain name configured here must be the same as the upstream before forwarding.

Proxy _ pass http:// host name;

proxy _ set _ header X-Real-IP $ remote _ addr;

}

# Enable nginx Status Monitoring Page

Location /nginxstatus {

stub _ status on

access _ log on

}

# Error page

error _ page 500 502 503 504/50x . html;

location = /50x.html {

Root html

}

}

}

At this point, nginx's basic load balancing configuration is completed. Two tomcat were deployed in the experiment, and then different results were returned when they were visited. When you enter the address in the browser, you can really see different results. The content of nginx configuration file needs further study.