日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2018.3.13 12周2次课

發布時間:2025/5/22 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2018.3.13 12周2次课 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

十二周二次課(3月13日)

12.6 Nginx安裝

12.7 默認虛擬主機

12.8 Nginx用戶認證

12.9 Nginx域名重定向

12.6 Nginx安裝

  • 下載和解壓:

cd /usr/local/src

wget http://nginx.org/download/nginx-1.13.9.tar.gz

tar -zxvf nginx-1.13.9.tar.gz

  • 配置編譯選項

cd nginx-1.13.9

./configure --prefix=/usr/local/nginx

  • 編譯和安裝

make && ?make install

  • 創建配置文件和啟動腳本

vim /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.1 //不用系統自帶的,我們自己建立以新的。也可以清空原來的配置文件,用命令:>/usr/local/nginx/conf/nginx.conf

vim nginx.conf

  • 啟動

service nginx start

  • 測試

curl localhost

能訪問到時因為在配置文件 nginx.conf里默認主機頁里,設置了如下的設置

  • 測試php解析

我們在配置文件里也配置了解析php:

vi /usr/local/nginx/html/1.php //加入如下內容

<?php

echo "This is nginx test page.";

?>

12.7 默認虛擬主機

在nginx中也又默認虛擬主機,跟httpd類似,第一個被nginx加載的虛擬主機就是默認主機。但和httpd不相同的地方時,它還有一個配置用來標記默認虛擬主機。也就是說,如果沒有這個標記,第一個虛擬主機為默認虛擬主機。

修改主配置文件:vim /usr/local/nginx/conf/nginx.conf //注釋掉server。在結束符號 } 上面增加一行:include vhost/*.conf; 。

這樣/usr/local/nginx/conf/vhost/下面所有以.conf結尾的文件都會加載。我們就可以包所有虛擬主機配置文件放到vhost目錄下面

vhost目錄在conf目錄下,如果沒有就創建一個,

vim aaa.com.conf //編輯虛擬主機aaa.com配置文件

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/ //創建default目錄

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 //重新加載服務,好處是當配置文件里有錯誤時是不會生效的

12.8 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; //用戶名密碼文件所在位置

}

}

要用到apache的htpasswd文件來創建用戶名密碼文件

/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming

創建第二個:/usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1

-t && -s reload //測試配置并重新加載

測試:

curl -x127.0.0.1:80 test.com -I //狀態碼為401,說明訪問的內容需要用戶認證

curl -x127.0.0.1:80 -uaming:123456 test.com //狀態碼404,因為沒有創建index.html

創建目錄:mkdir /data/wwwroot/test.com

創建index.html:echo “test.com”>/data/wwwroot/test.com/index.html

  • 訪問目錄時需要認證:目錄名admin

vim /usr/local/nginx/conf/vhost/test.com.conf //編輯test.com.conf

server

{

? ?listen 80;

? ?server_name test.com;

? ?index index.html index.htm index.php;

? ?root /data/wwwroot/test.com;

? ?location ?/admin/ //更改目錄名字為admin

? ?{

? ? ? ?auth_basic ? ? ? ? ? ? ?"Auth";

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

? ?}

}

訪問網站時,不需要指定用戶名和密碼

訪問admin目錄時,需要用戶名和密碼

創建admin目錄:mkdir /data/wwwroot/test.com/admin

創建admin目錄下的index.htm:echo “test.com admin dir”>/data/wwwroot/test.com/admin/index.html

  • 訪問文件時需要認證:文件名admin,php

vim /usr/local/nginx/conf/vhost/test.com.conf //編輯test.com.conf

server

{

? ?listen 80;

? ?server_name test.com;

? ?index index.html index.htm index.php;

? ?root /data/wwwroot/test.com;

? ?location ?~ admin.php //~:匹配,匹配admin.php

? ?{

? ? ? ?auth_basic ? ? ? ? ? ? ?"Auth";

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

? ?}

}

訪問目錄時,不需要指定用戶名和密碼

訪問文件時,需要指定用戶名和密碼

12.9 Nginx域名重定向

Nginx的域名重定向和httpd的類似

更改配置文件:vim /usr/local/nginx/conf/vhost/test.com.conf

server

{

? ?listen 80;

? ?server_name test.com test1.com test2.com; // server_name后面支持寫多個域名。httpd:ServerName后面只有1個域名,ServerAlias后面跟多個域名,

? ?index index.html index.htm index.php;

? ?root /data/wwwroot/test.com;

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

? ? ? ?rewrite ?^/(.*)$ ?http://test.com/$1 ?permanent; permanent為永久重定向,狀態碼為301,如果寫redirect則為302

? ?}

}

測試:

查看狀態碼:301,

重定向到(最后一行):


轉載于:https://blog.51cto.com/415326/2086111

總結

以上是生活随笔為你收集整理的2018.3.13 12周2次课的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。