一、訪問控制
1.生成密碼認證文件(htpasswd)
yum -y install httpd-tools //安裝httpd-tools提供htpasswd命令
htpasswd -c /usr/local/nginx/conf/.hehe hehe //生成密碼認證文件,用戶hehe
chown nginx /usr/local/nginx/conf/.hehe && chmod 400 /usr/local/nginx/conf/.hehe //設置文件權限
2.修改配置文件,添加認證選項
vim /usr/local/nginx/conf/nginx.conf
location /status { //Server配置項下增加
stub_status on;
access_log off;
auth_basic "secret"; //基本認證
auth_basic_user_file /usr/local/nginx/conf/.hehe; //指定用戶認證配置文件路徑
}
3.重啓服務,測試
/etc/init.d/nginx restart
二、Nginx虛擬主機
實現方式
-
基於域名:不同域名、相同IP、相同端口
-
基於IP:不同域名、不同IP、相同端口
- 基於端口:不同域名、不同IP、不同端口
1.基於域名
(1)新建測試文件
mkdir /usr/local/nginx/html/www && mkdir /usr/local/nginx/html/image
echo "www is www" >/usr/local/nginx/html/www/index.html
echo "image is image" >/usr/local/nginx/html/image/index.html
(2)編輯nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
111 server {
112 listen 80;
113 server_name www.hiahia.com;
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }
121 server {
122 listen 80;
123 server_name image.hiahia.com;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }
(3)啓動服務
nginx -t //驗證配置文件是否有誤
/etc/init.d/nginx restart //重啓Nginx服務
2.基於IP
(1)新增網卡,設置IP,添加對應的DNS解析A記錄
(2)編輯nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
111 server {
112 listen 192.168.1.10:80;
113 server_name www.hiahia.com;
114
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }
121 server {
122 listen 192.168.1.20:80;
123 server_name image.hiahia.com;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }
(3)啓動服務
nginx -t //驗證配置文件是否有誤
/etc/init.d/nginx restart //重啓Nginx服務
3.基於端口
(1)編輯nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
111 server {
112 listen 192.168.1.10:82;
113 server_name www.xueluo.org;
114
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }
121 server {
122 listen 192.168.1.20:83;
123 server_name image.xueluo.org;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }
(2)啓動服務
nginx -t //驗證配置文件是否有誤
/etc/init.d/nginx restart //重啓Nginx服務