1. 程式人生 > >47.Nginx安裝、默認虛擬主機、Nginx用戶認證、Nginx域名重定向

47.Nginx安裝、默認虛擬主機、Nginx用戶認證、Nginx域名重定向

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

一 、Nginx安裝
cd /usr/local/src
 wget http://nginx.org/download/nginx-1.12.1.tar.gz
 tar  zxf nginx-1.12.1.tar.gz
 cd nginx-1.12.1
 ./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
 netstat -lntp |grep 80

技術分享圖片

技術分享圖片

技術分享圖片

  • 測試php解析
vi /usr/local/nginx/html/1.php      //加入如下內容
 <?php
    echo "test php scripts.";
?>

 curl localhost/1.php

技術分享圖片

技術分享圖片

技術分享圖片

二、Nginx默認虛擬主機

  vim /usr/local/nginx/conf/nginx.conf  //先刪除server部分,然後增加
  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/
 echo “This is a default site.”>/data/wwwroot/default/index.html
 /usr/local/nginx/sbin/nginx -t
 /usr/local/nginx/sbin/nginx -s reload     //不重啟nginx重新加載配置文件,如果配置文件錯誤則不會重新加載
 curl localhost
 curl -x127.0.0.1:80 123.com

技術分享圖片

技術分享圖片

技術分享圖片

三、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  //如果沒有安裝apache就yum安裝,然後執行下面命令
 htpasswd -c /usr/local/nginx/conf/htpasswd chinantfy
  • 由於之前已經安裝apache,這裏直接執行命令
 /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd  chinantfy //第一次生成文件才需要加-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
 curl -x127.0.0.1:80 test.com -I//狀態碼為401說明需要驗證
 curl -uchinantfy:qwer -x127.0.0.1:80 test.com -I   //訪 問狀態碼變為200 

技術分享圖片

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

技術分享圖片

  • 針對目錄的用戶認證(test/admin)
location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
  • 針對php文件
location  ~ admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

四、Nginx域名重定向

vim /usr/local/nginx/conf/vhost/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;
    }
}
  • server_name後面支持寫多個域名,這裏要和httpd的做一個對比,httpd只能寫一個
 /usr/local/nginx/sbin/nginx -t
 /usr/local/nginx/sbin/nginx -s reload
 curl -x127.0.0.1:80 test1.com -I

permanent為永久重定向,狀態碼為301,如果寫redirect則為302

技術分享圖片

47.Nginx安裝、默認虛擬主機、Nginx用戶認證、Nginx域名重定向