Nginx configuration and load balancing implementation

What is Nginx?

Nginx (engine x) is a high performance HTTP and reverse proxy web server. It is characterized by less memory and strong concurrency capabilities. In fact, the concurrency capability of nginx is better than other web servers of the same type.

Capable of supporting responses up to 50,000 concurrent connections . The Nginx code is completely written from scratch in C language and can be ported to many operating systems: Linux , Mac OS , Microsoft Windows , etc.

Detailed Nginx configuration and load balancing

#Overview; it is divided into three parts

############################# Part 1 Global Configuration #################### ########

#user nobody; Specify the user to start the process, it is not necessary to specify by default

#error_log logs/error.log; Configure the log output, although it is called error_log, but you can define the output level, the default is not to write the error level

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid; The file that records the pid is placed in this location by default

# Only start one process, nginx is a multi-process single-threaded model

worker_processes 1;

############################# The second part event configuration ################### #########

#Mainly related to network connection configuration

events {

# Each worker can connect to 1024 links

worker_connections 1024;

# epoll; Event-driven model, multiplexing IO multiplexing

}

############################## Part III http configuration ################### #########

http {

include mime.types;

default_type application/octet-stream;#text/html The default return type can also be modified in server.location

sendfile on; #Open sendfile system call

keepalive_timeout 65; #Connection timeout is 65s

upstream peter {

#Server resources

server 10.73.25.10:8080 weight=1

server 10.73.25.11:8080 weight=1

}

#upstream peter {

## Server resources

#server 127.0.0.1:8080 weight=2

#server 127.0.0.1:8081 weight=1

#}

#upstream peter {

## Server resources

#server 127.0.0.1:8080 weight=3

#server 127.0.0.1:8081 weight=1

#}

server {

listen 80;

server_name localhost;

location /demo0 {

root html;

index index.html index.htm;

proxy_pass http://peter;

}

# The following shows multiple demos. The demos do not depend on each other and can be configured separately for testing.

################## demo1 shows different writing priority of location path ######################### ###

# = the highest priority, indicating that the path is exactly equal, and can match the request of demo1/a/b

location /demo1/a/b {

return "/demo1/a/b"

}

# ^~ The second highest, table startWith, can match the request of demo1/abbb demo1/a/bbbb

location /demo1/a {

return "/demo1/a"

}

# ~ 第三高,表示正则,注意加双引号

location "/demo1/\w{3}$" {

return "regex"

}

# 最低优先级,没有任何前置符号

location /demo1 {

return "/demo1"

}

################## demo2 展示rewrite跳转 ##########################

location /demo2 {

rewrite ^/(.*) /$1/api permanent # permanent 301, redirect 302

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

# 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;

# }

#}

}

Guess you like

Origin blog.csdn.net/A_captain_608/article/details/129713140