作为一个南方人,我每天都洗澡,一年四季,从不间断。甚至我在北京读书的7年,都这么过来的(除开几次喝醉的情况)。洗澡是一件很舒服的事情,你可以完全放松,听着music,哼着小曲,多么惬意。阿基米德也是在洗澡的时候发现了浮力,以此证明:洗澡是件好事!
今天在洗澡的时候,我突然领悟到了nginx的反向代理是干嘛的,于是我哐哧哐哧的研究了一晚上怎么配置nginx。以下上干货!
一、安装(ubuntu16.04)
(1)下载
wget wget http://nginx.org/download/nginx-1.17.8.tar.gz(版本详情请移步nginx官网)
(2)安装环境
sh:apt update
sh:apt install libpcre3 libpcre3-dev openssl libssl-dev zlib*
(3)解压(我的路径/opt/nginx/)
tar -zxf nginx-1.17.8.tar.gz
(4)编译
cd nginx-1.17.8
./configure --prefix=./main --sbin-path=/usr/local/nginx/sbin/nginx
make & make install
(5)配置文件
cd main/conf & vim my_conf.conf
二、配置nginx
#user nobody;
worker_processes 2;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
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 on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
gzip on;
#当返回内容大于此值时才会使用gzip进行压缩,以K为单位
gzip_min_length 2k;
#申请32 * 4K内存页
gzip_buffers 32 4k;
gzip_http_version 1.0;
#设置gzip压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大
gzip_comp_level 3;
#压缩类型
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
#http_proxy
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /opt/nginx/nginx-1.17.8/main/logs 1 2;
upstream myservers {
#集群共享内存的大小
zone upstream_dynamic 64k;
#weight:权重
server 172.26.91.177:2331 weight=5;
#fail_timeout:超过5s(默认10s)标记为失联,slow_start:30s内权重逐渐恢复至正常
#server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
#尝试重连次数3次(默认每次10s)
#server 192.0.2.1 max_fails=3;
#自动监控backend3.example.com 域名ip的变化
#server backend3.example.com resolve;
#备份服务器
server 172.26.91.178:2332 backup;
#server backup2.example.com:8080 backup;
}
upstream myservers2 {
#集群共享内存的大小
zone upstream_dynamic 64k;
#weight:权重
server 172.26.91.177:2333 weight=5;
#fail_timeout:超过5s(默认10s)标记为失联,slow_start:30s内权重逐渐恢复至正常
#server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
#尝试重连次数3次(默认每次10s)
#server 192.0.2.1 max_fails=3;
#自动监控backend3.example.com 域名ip的变化
#server backend3.example.com resolve;
#备份服务器
server 172.26.91.178:2334 backup;
#server backup2.example.com:8080 backup;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /web {
proxy_pass http://myservers;
}
location /test {
proxy_pass http://myservers2;
}
#
#location /test {
# 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# 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;
# }
#}
}
以上配置,我一个一个给大家解释,大家以后配置nginx,都可以根据以上做修改
我有两个服务器,分别是172.26.91.177和172.26.91.178,其中每个ip开了两个服务:
(1):http://172.26.91.177:2331/web1 和 http://172.26.91.177:2333/test1 ,分别模拟两个类型的服务
(2):http://172.26.91.178:2332/web1 和 http://172.26.91.178:2334/test1 ,分别作为177的备用机器
1、worker_processes
进程数,nginx处理任务时调用的进程数,理论上大一些肯定速度要快一些
2、error_log
日志名字和路径
3、pid
进程文件
4、events
(1)use epoll
使用epoll批处理方法
(2)worker_connections 1024
最大连接数
(3)multi_accept on
允许一个进程同时接受多个网络连接
5、http
(1)include mime.types
MIME.type解释
(2)default_type application/octet-stream
普通文件流
(3)sendfile on
开启高效文件传输模式
(4)keepalive_timeout 60
请求超时时间
(5)压缩
gzip on;
#当返回内容大于此值时才会使用gzip进行压缩,以K为单位
gzip_min_length 2k;
#申请32 * 4K内存页
gzip_buffers 32 4k;
#向上兼容
gzip_http_version 1.0;
#设置gzip压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大
gzip_comp_level 3;
#压缩类型
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
#开启根据请求头判断是否压缩
gzip_vary on;
(6)代理(看字识义。。。)
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /opt/nginx/nginx-1.17.8/main/logs 1 2;
(7)负载均衡核心配置1(服务器配置)
upstream myservers {
#集群共享内存的大小
zone upstream_dynamic 64k;
#weight:权重
server 172.26.91.177:2331 weight=5;
#fail_timeout:超过5s(默认10s)标记为失联,slow_start:30s内权重逐渐恢复至正常
#server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
#尝试重连次数3次(默认每次10s)
#server 192.0.2.1 max_fails=3;
#自动监控backend3.example.com 域名ip的变化
#server backend3.example.com resolve;
#备份服务器
server 172.26.91.178:2332 backup;
#server backup2.example.com:8080 backup;
}
upstream myservers2 {
#集群共享内存的大小
zone upstream_dynamic 64k;
#weight:权重
server 172.26.91.177:2333 weight=5;
#fail_timeout:超过5s(默认10s)标记为失联,slow_start:30s内权重逐渐恢复至正常
#server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
#尝试重连次数3次(默认每次10s)
#server 192.0.2.1 max_fails=3;
#自动监控backend3.example.com 域名ip的变化
#server backend3.example.com resolve;
#备份服务器
server 172.26.91.178:2334 backup;
#server backup2.example.com:8080 backup;
}
1)upstream声明进行配置,myservers自定义别名,代表一个配置(服务)的名称
2)zone upstream_dynamic 64k
共享内存大小
3)其余见注释备注
(8)负载均衡核心配置2(代理规则)
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /web {
proxy_pass http://myservers;
}
location /test {
proxy_pass http://myservers2;
}
#
#location /test {
# 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
上面两个分块的配置,我是要达到如下要求:
主服务器(177),有两个服务,请求的url分别为:
http://172.26.91.177:2331/web1 http://172.26.91.177:2333/test1
如果访问web1子路径,则nginx转发到2331端口,如果访问test1子路径,则转发到2333端口
另外,如果以上服务宕了,
http://172.26.91.178:2332/web1 http://172.26.91.178:2334/test1
分别作为177的备用机器
这样,就把备份和负载均衡全部设置在了一起,配合容器编排,能毫无压力的搭建高并发的web服务!
这样的nginx模板能做到:
服务转发到相同机器的相同端口的不同子域名
服务转发到相同机器的不同端口的不同子域名
服务转发到相同机器的不同端口的相同子域名
服务转发到不同机器的相同端口的不同子域名
服务转发到不同机器的不同端口的不同子域名
服务转发到不同机器的不同端口的相同子域名
服务转发到不同机器的相同端口的相同子域名
你就说厉不厉害吧!!!
三、nginx常用命令
(1)启动(指定配置文件,推荐)
nginx -c xxxxx/my.conf
(2)重载(一般用于修改文件后)
nginx -s reload
(3)关闭
nginx -s quit