Nginx基礎知識

小瞿慢慢跑發表於2020-12-17

nginx學習筆記

安裝

yum方式安裝openssl、pcre、zlib

yum -y install openssl openssl-devel
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel 

下載安裝Nginx(安裝目錄在/usr/local/nginx,安裝ssl模組)
wget http://nginx.org/download/nginx-1.15.0.tar.gz
tar -zxvf nginx-1.15.0.tar.gz
cd nginx-1.15.0
./configure --sbin-path=/usr/local/nginx/nginx --with-http_ssl_module
make && make install

啟動

檢視nginx的pid
 cd usr/local/nginx/logs
 cat nginx.pid
 
 1、檢視 nginx版本號 
 ./nginx -v
2、啟動 nginx 
./nginx 
3、停止 nginx 
./nginx -s stop 
 4、重新載入
 ./nginx -s reload

轉發

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       9000;
        server_name  100.100.100.4;
        location ~ /edu/ {
            proxy_pass   http://100.100.100.4:8081;
        }
		location ~ /vod/ {
            proxy_pass   http://100.100.100.4:8082;
        }
    }
}

負載均衡

nginx 負載均衡

1.輪詢
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

   upstream xiaoquserver{
		server 100.100.100.4:8081;
		server 100.100.100.4:8082;
   }

    server {
        listen       9000;
        server_name  100.100.100.4;

        location  / {
            root   html;
            proxy_pass   http://xiaoquserver;
            index  index.html index.htm;
        }

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

}
2.權重
   upstream xiaoquserver{
		server 100.100.100.4:8081 weight=1;
		server 100.100.100.4:8082 weight=2;
   }
3.ip hash
   upstream xiaoquserver{
   		ip_hash;
		server 100.100.100.4:8081 weight=1;
		server 100.100.100.4:8082 weight=2;
   }
 
4. 最少連線 分配到最少連線上
    upstream xiaoquserver{
   		least_conn;
		server 100.100.100.4:8081 weight=1;
		server 100.100.100.4:8082 weight=2;
   }
   
5.後端伺服器的響應時間來分配請求,響應時間短的優先分配
    upstream xiaoquserver{
		server 100.100.100.4:8081 weight=1;
		server 100.100.100.4:8082 weight=2;
		fair; 
   }