Nginx安裝.默認虛擬主機... 原

11月26日任務

12.6 Nginx安裝
12.7 默認虛擬主機
12.8 Nginx用戶認證
12.9 Nginx域名重定向

1.Nginx安裝

示例一:

  • cd /usr/local/src  進入目錄下
  • wget http://nginx.org/download/nginx-1.12.1.tar.gz  下載Nginx包
  •  tar zxf nginx-1.12.1.tar.gz  解壓包
  • ./configure --prefix=/usr/local/nginx  編譯
  • make &&  make install  
  • 查看一下各個目錄下的文件

  • vim /etc/init.d/nginx //複製如下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )在該網址複製
  • chmod 755 /etc/init.d/nginx  給權限
  • chkconfig --add nginx  加入服務列表
  • chkconfig nginx on 開啓開機啓動
  • cd /usr/local/nginx/conf/  進入目錄下
  • mv nginx.conf nginx.conf.bak  給默認的配置文件模板改名備份
  • vim nginx.conf // 自己創建一個配置文件,寫入如下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)去該網址複製一份
  •  /usr/local/nginx/sbin/nginx -t  檢查腳本是否有語法錯誤
  • /etc/init.d/nginx  start  啓動Nginx服務
  • ps aux | grep nginx 查看一下進程
  • netstat -lntp |grep 80  查看一下監聽端口

  • 用curl 測試一下

  • 測試PHP解析
  • vi /usr/local/nginx/html/1.php //創建一個php文檔,加入如下內容  
  • <?php    
  • echo "test php scripts.";
  • ?>  
  • curl localhost/1.php 測試一下,如下解析成功

2.Nginx默認虛擬主機

示例一:

  • vim /usr/local/nginx/conf/nginx.conf //增加
  •  include vhost/*.conf  這是要添加的內容

  • mkdir /usr/local/nginx/conf/vhost  創建目錄
  • cd !$ 進入目錄下
  • vim default.conf //編輯文件,加入如下內容

server

{    

listen 80 default_server;  // 定義它的監聽端口,有這個標記的就是默認虛擬主機    

server_name aaa.com;    域名

index index.html index.htm index.php;    指定索引頁

root /data/wwwroot/default; 指定網址位置

}  

  • mkdir -p /data/wwwroot/default/  創建目錄
  • cd !$ 進入目錄下
  • echo “This is a default  site.”>/data/wwwroot/default/index.html 創建一個index.html文件並寫入一些內容
  •  /usr/local/nginx/sbin/nginx -t 檢查一下
  •  /usr/local/nginx/sbin/nginx -s reload  重新加載一下配置文件
  • curl localhost  測試訪問一下

  • curl -x127.0.0.1:80 123.com 指定一個域名,測試訪問一下

  • 不管訪問什麼域名,只要解析指向到這個服務器,都會訪問到該站點,這就是默認虛擬主機

 

3.Nginx用戶認證

示例一:用戶認證

  • vim /usr/local/nginx/conf/vhost/test.com.conf//創建一個虛擬主機,寫入如下內容

server

{    

listen 80;

server_name test.com;    

index index.html index.htm index.php;    

root /data/wwwroot/test.com;    

location  /    

{        

auth_basic              "Auth";         用戶認證的名字

auth_basic_user_file   /usr/local/nginx/conf/htpasswd; 用戶名密碼文件

}

}  

  • yum install -y httpd  yum安裝httpd,使用htpasswd工具
  • htpasswd -c /usr/local/nginx/conf/htpasswd aming 生成用戶密碼,-c是生成的意思,如果生成第二個則不需要用-c,不然會重置
  • /usr/local/nginx/sbin/nginx -t 檢查語法是否有錯誤
  • /usr/local/nginx/sbin/nginx -s reload //測試配置並重新加載配置文件
  • mkdir /data/wwwroot/test.com 創建目錄
  •  echo “test.com”>/data/wwwroot/test.com/index.html 建立一個html文件
  •  curl -x127.0.0.1:80 test.com -I//狀態碼爲401說明需要驗證  
  • curl -uaming:passwd -x127.0.0.1:80 test.com 訪問狀態碼變爲200  

  • 編輯windows的hosts文件,然後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗  

示例二:針對目錄的用戶認證

  • vi test.com.conf 編輯文件,做如下更改

location  /admin/    

{        

auth_basic              "Auth";        

auth_basic_user_file   /usr/local/nginx/conf/htpasswd;

}

  • 用curl 測試

  • 新建一個測試html文件,並測試一下

示例三:針對php做限制,如下更改

  • vi test.com.conf 編輯文件

 

 

4.Nginx域名重定向

示例一:

  • 更改test.com.conf 編輯文件:

server ,如下

{    

listen 80;    

server_name test.com test1.com test2.com;  配置域名,而且可以配置多個域名

index index.html index.htm index.php;    

root /data/wwwroot/test.com;    

if ($host != 'test.com' )

{        

rewrite  ^/(.*)$  http://test.com/$1  permanent;    指定調整到哪個域名下,permanent指定301狀態碼

   }

}  

  • server_name後面支持寫多個域名,這裏要和httpd的做一個對比  permanent爲永久重定向,狀態碼爲301,如果寫redirect則爲302
  • 檢查語法並重新加載配置文件

  • curl 測試一下,任意域名都會重定向到指定域名下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章