nginx源码安装-centos7
安装平台:centos7.3
1、下载包到指定目录
wget http://nginx.org/download/nginx-1.16.1.tar.gz -P /tmp
2、解压包
tar -zxvf /tmp/nginx-1.16.1.tar.gz -C /tmp
3、安装依赖包
yum -y install gcc yum -y install zlib-devel yum -y install pcre-devel yum -y install openssl-devel
4、进行nginx配置编译安装
cd /tmp/nginx-1.16.1 ./configure make make install
5、进入默认路径启动
cd /usr/local/nginx/sbin ./nginx
6、网页访问ip (不能访问先关闭selinux、iptables)
setenforce 0 #临时管理selinux systemctl stop firewalld
7、改端口
cd /usr/local/nginx/conf vi nginx.conf
8、配置开机启动
echo ‘/usr/local/nginx/sbin/nginx‘>>/etc/rc.local chmod u+x /etc/rc.local
9、nginx配置简单的负载均衡
cat <<EOF >/usr/local/nginx/conf/nginx.confworker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { proxy_pass http://192.168.1.10:8080; proxy_pass http://192.168.1.10:8081; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }EOF
下面介绍一些常用配置
10、正常代理
// 下面这些代码的意思是将本机80号端口接受来的消息转发给 192.168.10.10 的 80 号端口 upstream www.wukc.com{ //这里的域名随便填,但是要和下面的对应 server 192.168.10.10:80; //真正提供服务的服务器的ip地址和端口 } server{ listen 80; // 监听80号端口发过来的消息 location /{ proxy_pass http://www.wukc.com; index index.html index.php; } }
11、根据不同端口进行转发
//这些代码的意思是将80端口接收到的信息转发给 192.168.10.10的80端口,而将接受到的 8080端口的信息转发给 192.168.10.20的8080端口 upstream www.wukc.com{ server 192.168.10.10:80; } upstream www.wukc2.com{ server 192.168.10.20:8080; } server{ listen 80; location /{ proxy_pass http://www.wukc.com; index index.html index.php; } } server{ listen 8080; location /{ proxy_pass http://www.wukc2.com; index index.html index.jsp; } }
12、四种负载均衡
1:轮询:每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除 upstream www.wukc.com{ server 192.168.10.10:80; server 192.168.10.20:80; } server{ listen 80; location /{ proxy_pass http://www.wukc.com; index index.html index.php index.jsp; } } 2:ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 upstream www.wukc.com{ ip_hash; server 192.168.10.10:80; server 192.168.10.20:80; } server{ listen 80; location /{ proxy_pass http://www.wukc.com; index index.html index.php index.jsp; } } 3:weight:指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 upstream www.wukc.com{ server 192.168.10.10:80 weight=10; server 192.168.10.20:80 weight=20; } server{ listen 80; location /{ proxy_pass http://www.wukc.com; index index.html index.php index.jsp; } } 4: fair : 按后端服务器的响应时间来分配请求,响应短的服务器优先分配 upstream www.wukc.com{ server 192.168.10.10:80 weight=10; server 192.168.10.20:80 weight=20; fair; } server{ listen 80; location /{ proxy_pass http://www.wukc.com; index index.html index.php index.jsp; } }
13、本地转发代理(基于不同后缀)
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } location ~ \.php$ { //当请求的后缀是以 .php结尾的话,将流量转发给本地的800端口 proxy_pass http://127.0.0.1:800; } location ~ \.jsp$ { //当请求的后缀是以 .jsp结尾的话,将流量转发给本地的8080端口 proxy_pass http://127.0.0.1:8080; } location ~ \.(jpg|png)$ { //当请求的后缀是以 .jpg或.png 结尾的话,则请求 /img 目录下 root /img; } }
相关推荐
张立军的架构之路 2020-09-28
yongzhang 2020-07-19
nginxs 2020-11-14
Guanjs0 2020-11-13
小木兮子 2020-11-11
yserver 2020-11-11
ssihc0 2020-11-11
windle 2020-11-10
HanksWang 2020-11-10
liuchen0 2020-11-10
Freshairx 2020-11-10
ccschan 2020-11-10
liwf 2020-11-10
Guanjs0 2020-11-09
AderStep 2020-11-09
zrtlin 2020-11-09