一、訪問控制
基于用戶名、密碼的驗證訪問
服務端配置
vim /usr/local/nginx/conf/nginx.conf
在想驗證的location下面添加,以根區域為例
location / {
root html;
index index.html index.htm;
#添加下面兩行
auth_basic "welcome you here";
auth_basic_user_file /usr/local/nginx/html/a.psd;
}

創建認證檔案,htpasswd是安裝包httpd-tools擁有的命令
cd /usr/local/nginx
htpasswd -c /usr/local/nginx/html/a.psd 訪問用戶名

允許個別、拒絕所有
vim /usr/local/nginx/conf/nginx.conf
在location下添加如下規則
allow 172.16.1.1; #允許訪問
deny 172.16.1.0/24; #拒絕1.0網段
重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
客戶端訪問測驗
172.16.1.1

172.16.1.10

二、虛擬主機(基于域名)
在主組態檔中,添加不同的server區域
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.zcy.com;
location / {
root html/zcy;
index index.html index.htm index.php;
}
}
server {
listen 80;
server_name www.qy.com;
location / {
root html/qy;
index index.html index.htm index.php;
}
}

新建站點和訪問界面、并賦予nginx權限
cd /usr/local/nginx/html
mkdir zcy
mkdir qy
echo "zcyzcyzcy~~~~~~~~~~" > zcy/index.html
echo "qyqyqy~~~~~~~~~~~~~" > qy/index.html
重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
客戶端測驗


三、域名跳轉(www.zcy.com–>www.qy.com)
基于上面的虛擬主機實驗
vim /usr/local/nginx/conf/nginx.conf
在www.zcy.com的location下修改添加跳轉
rewrite .* http://www.new.com permanent;

重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
客戶端測驗

四、實作https加密
在三的基礎上繼續實驗
實作對www.qy.com的https訪問
生成證書
cd /usr/local/nginx/conf
openssl genrsa -out cert.key 1024
建立服務器私鑰,生成RSA密鑰
openssl req -new -key cert.key -out cert.csr
openssl x509 -req -days 365 -sha256 -in cert.csr -signkey cert.key -out cert.pem
修改主組態檔,修改server埠為443、添加驗證配置
vim /usr/local/nginx/conf/nginx.conf
對www.qy.com添加ssl加密驗證
listen 443;
server_name www.qy.com;
ssl on;
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;

重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
客戶端測驗



五、埠跳轉(80 -> 443)
在四的基礎上繼續實驗
實作訪問www.qy.com自動跳轉到https://www.qy.com
修改主組態檔 添加如下內容
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.qy.com;
location / {
rewrite .* https://www.qy.com permanent;
}
}

重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
客戶端測驗




六、擴展:http2
在五的基礎上繼續實驗
安裝時有–with-http_v2_module引數
修改主組態檔
vim /usr/local/nginx/conf/nginx.conf
server{
#listen的值加上ssl、http2
listen 443 ssl http2;
server_name www.qy.com;
ssl on;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; ssl_prefer_server_ciphers on;
location / {
root html/qy;
index index.html index.htm index.php;
}
}

重啟測驗
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx

七、反向代理
在虛擬主機的基礎上實驗
主組態檔對應的location下面添加代理網站proxy_pass
vim /usr/local/nginx/conf/nginx.conf
server{
.... #其他配置
location / {
#此處填寫真實服務器的IP地址,代理其他主機
#也可填本機其他域名,實作跳轉
proxy_pass http://172.16.1.20:80;
}
}

重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
后臺真實服務器
yum -y install httpd
echo welcome to 172.16.1.20 >/var/www/html/index.html
systemctl start httpd
測驗機
訪問nginx所在機器的IP或域名,出現Apache里的界面內容

八、負載均衡
在七的基礎上實驗
主組態檔對應的location下面添加代理網站proxy_pass
vim /usr/local/nginx/conf/nginx.conf
#此標簽在server標簽前添加
upstream zcy {
#權重越大,訪問它的次數就越多
server 172.16.1.20:80 weight=1;
server 172.16.1.30:80 weight=1;
}
server {
listen 80;
server_name www.zcy.com;
location / {
proxy_pass http://zcy;
proxy_set_header Host $host;
}
}

重啟服務
/usr/local/nginx/sbin/nginx -t
pkill -HUP nginx
配置被代理的真實服務器
172.16.1.20
yum -y install httpd
echo welcome to 172.16.1.20 >/var/www/html/index.html
systemctl start httpd
172.168.1.30
yum -y install httpd
echo welcome to 172.16.1.30 >/var/www/html/index.html
systemctl start httpd
客戶端測驗
多次請求www.zcy.com,可以看到得到的資料一次來自172.16.1.20、一次來自172.16.1.30


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/303560.html
標籤:其他
上一篇:HTML的簡介、標簽屬性
