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
方法二:
[root@localhost ]# cat << EOF > /lib/systemd/system/nginx.service #创建Nginx服务系统启动文件
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl start nginx && \
systemctl enable nginx && systemctl status nginx

9、nginx配置简单的负载均衡

cat </usr/local/nginx/conf/nginx.conf
worker_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;
    }
}

 14、不同访问路径转发到不同服务配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#配置两个upstream,转发到不同服务
upstream kubesphere{
   server 172.21.210.10:30880;
}
upstream harbor{
   server 172.21.210.20:80;
}

    server {
        listen       443;
        server_name  localhost;
        location /kubesphere {              #访问路径可以和upstream不一样
            proxy_pass http://kubesphere;   #该名称为上方upstream名称,转发到上面的upstream
        }
       
        location /harbor {
            proxy_pass http://harbor;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

 

你可能感兴趣的:(nginx源码安装-centos7)