Because most of the services in the current project use nginx to balance the load, it is recorded here for future reference.
as follows:
Nginx installation directory: /root/softwate/nginx
nginx executable directory: /root/softwate/nginx/sbin/nginx
Nginx configuration file directory: /root/softwate/nginx/conf/nginx.conf
The configuration file of nginx is as follows:
#用户,一般不用配置,我测试用的root 需要指定下root
user root;
#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;
events {
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
}
http {
#设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
#设置日志格式
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 logs/access.log main;
#sendfile sendfile: 设置为on表示启动高效传输文件的模式
#对于普通应用,设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer
#用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer
#再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket
#开启这个参数后可以让数据不用经过用户buffer
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
#upstream服务器,轮询机制,均衡负载,以权重的方式分发,weight是权重
#权重越大则分配的可能行越大
#这里后端服务只有一个,所以都会分发到哪一个上
upstream test {
server 172.21.0.5:8081 weight=10;
}
#虚拟主机配置
server {
#监听80端口
listen 80;
#定义访问地址,可以有多个,用空格分隔
server_name localhost;
#charset koi8-r;
#虚拟主机的访问日志
#access_log logs/host.access.log main;
#默认请求
location / {
#root html;
#默认访问目录
root /root/html;
#指定默认访问页面
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#配置访问静态文件
#配置访问url中包含test的静态文件
location ~ ^/test/.*\.(gif|bmp|jpg|jpeg|png|swf|GIF|BMP|JPG|JPEG|PNG)$ {
#文件目录
root /root/html;
}
#启用反向代理,配置服务
#分发处理带有test的url请求
location ~ /(test)/.*$ {
#分发到upstream服务test ,也可以直接指定http://ip:port 如172.21.0.5:8081
proxy_pass http://test;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $host:$server_port;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header connect "";
#以下可选,为反向代理配置
client_max_body_size 30m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 1024k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 60; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 120; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 120; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 128k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 8 128k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 128k; #高负荷下缓冲大小(一般proxy_buffers*2)
}
}
}
The operation command of the nginx service:
The following commands are operated according to the nginx directory I installed, and the specific modifications should be based on their own actual changes.
1. Verify whether there is a problem with the configuration of nginx. Before starting nginx or reloading the ng configuration, check whether the nginx configuration file is normal. Especially online, the ng configuration is generally reloaded to prevent problems and must be detected.
命令:/root/softwate/nginx/sbin/nginx -t -c /root/softwate/nginx/conf/nginx.conf
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -t -c /root/softwate/nginx/conf/nginx.conf
nginx: the configuration file /root/softwate/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /root/softwate/nginx/conf/nginx.conf test is successful
[root@VM_0_5_centos sbin]#
2. Start nginx.
命令:/root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf
After startup, there is generally no prompt, you can view the nginx process by yourself
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf
[root@VM_0_5_centos sbin]#
[root@VM_0_5_centos sbin]# ps -ef | grep nginx
root 30262 1 0 10:32 ? 00:00:00 nginx: master process /root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf
root 30263 30262 0 10:32 ? 00:00:00 nginx: worker process
root 30267 29966 0 10:33 pts/0 00:00:00 grep --color=auto nginx
[root@VM_0_5_centos sbin]#
3. Reload the nginx configuration. Generally, after modifying the nginx configuration, you can reload it without restarting.
命令:root/softwate/nginx/sbin/nginx -s reload
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -s reload
[root@VM_0_5_centos sbin]#
After reloading, there is generally no direct prompt, but in the log of ng, there will be a log record. In short, if you are not familiar with it, you must make a backup and check the configuration file before modifying it online.
test:
In the default access directory /root/html of nginx, the index.html file is generated. It's just a test, whatever. . .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nginx测试首页</title>
</head>
<body>
<p>这是ng访问测试</p>
</body>
</html>
Then, the browser accesses the external network IP and port number 80 corresponding to ng (the port can also be omitted). The browser displays the nginx test home page normally, indicating that the configuration is normal.
The load balance test of other services will be coordinated with their own services, so I won't put them here.