溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Nginx服務器的搭建及配置

發(fā)布時間:2020-08-08 21:49:52 來源:網(wǎng)絡 閱讀:1054 作者:運維的名義 欄目:建站服務器

Linux 搭建Nginx服務 

1.Nginx安裝

2.Nginx虛擬主機

3.Nginx用戶訪問控制、進行SSL加密


一、構建Nginx服務器


1)使用源碼包安裝nginx軟件包

[root@svr ~]# yum -y install zlib-devel  openssl-devel  pcre-devel  perl-devel   make  gcc       //安裝常見依賴包

[root@svr ~]# useradd –s /sbin/nologin nginx

[root@svr ~]# wget http://124.202.164.12/files/408500000A3B37E2/nginx.org/download/nginx-1.13.1.tar.gz

[root@svr ~]# tar  -zxvf   nginx-1.13.1.tar.gz

[root@svr ~]# cd  nginx-1.13.1

[root@svr nginx-1.13.1]# ./configure   \

>--prefix=/usr/local/nginx   \         //指定安裝路徑

>--user=nginx   \                     //指定用戶

>--group=nginx  \                    //指定組

>--with-http_stub_status_module  \  //開啟狀態(tài)統(tǒng)計功能

>--with-http_ssl_module            //開啟SSL加密功能


....

[root@svr nginx-1.13.1]# make && make install    //編譯并安裝


2)啟用nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx  //啟動Nginx


nginx服務默認通過TCP 80端口監(jiān)聽客戶端請求:

[root@svr ~]# netstat  -anptu  |  grep nginx

tcp        000.0.0.0:800.0.0.0:*        LISTEN        10441/nginx


3)為Nginx Web服務器建立測試首頁文件

Nginx Web服務默認首頁文檔存儲目錄為/usr/local/nginx/html/,在此目錄下建立一個名為index.html的文件:

[root@svr ~]# cat  /usr/local/nginx/html/index.html


<h2>Welcome to Nginx 192.168.4.5 </h2>



二、用戶認證及訪問控制




通過Nginx實現(xiàn)Web頁面的認證與訪問控制,需要修改Nginx配置文件,在location容器中添加allow及deny語句實現(xiàn)訪問控制,

添加auth語句實現(xiàn)用戶認證。最后使用htpasswd命令創(chuàng)建用戶及密碼即可。




 修改Nginx配置文件

1)修改/usr/local/nginx/conf/nginx.conf

[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf

....

server {

        listen       80;

        server_name  localhost;

        auth_basic "Input Password:";  //認證提示符

        auth_basic_user_file pass.txt;  //認證密碼文件

location /{

            root   html;

            index  index.html index.htm;

}

location /test {

                allow 192.168.4.205;   //訪問控制,僅192.168.4.205可以訪問

                deny all;    //拒絕所有

            index  index.html index.htm;

}

2)創(chuàng)建二級頁面目錄,并生成index.html文件

[root@svr ~]# mkdir /usr/local/nginx/html/test

[root@svr ~]# echo “test” >/usr/local/nginx/html/test/index.html


3)生成密碼文件,創(chuàng)建用戶tom及密碼


使用htpasswd命令創(chuàng)建賬戶文件,需要確保系統(tǒng)中已經(jīng)安裝了httpd-tools。

[root@svr ~]# htpasswd -cm /usr/local/nginx/conf/pass.txt tom

New password:

Re-type new password:

Adding password for user tom


4)重啟Nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx –s stop

[root@svr ~]# /usr/local/nginx/sbin/nginx


客戶端測試

1)登錄192.168.4.205主機進行測試

 http://192.168.4.5                    //輸入密碼后可以訪問

 http://192.168.4.5/test            //輸入密碼后可以訪問

 

2)登錄非192.168.4.205的其他任意主機測試

 http://192.168.4.5                //輸入密碼后可以訪問

 http://192.168.4.5/test        //輸入密碼后無法訪問

 

 

 

三、 Nginx虛擬主機



實現(xiàn)兩個基于域名的虛擬主機,域名分別為www.tarena.com和bbs.tarena.com

域名為bbs.tarena.com的Web服務僅允許192.168.4.205訪問


對域名為bbs.tarena.com的站點進行用戶認證,用戶名稱為tom,密碼為123456


對域名為www.tarena.com的站點進行SSL加密



修改Nginx配置文件,添加server容器實現(xiàn)虛擬主機功能;對于需要進行訪問控制的虛擬主機添加allow和deny語句;

對于需要進行用戶認證的虛擬主機添加auth認證語句;對于需要進行SSL加密處理的站點添加ssl相關指令。



1)修改Nginx服務配置,添加相關虛擬主機配置如下

[root@svr ~]# vim /usr/local/nginx/conf/nginx.conf

....

server {

        listen       192.168.4.5:80;  //端口

        server_name  bbs.tarena.com;  //域名

allow 192.168.4.205;           //僅192.168.4.205可以訪問

deny all;                      //拒絕所有 

auth_basic "Input Password:";  //認證提示符

        auth_basic_user_file pass.txt;  //認證密碼文件

location /{

            root   bbs;//指定網(wǎng)站根路徑

            index  index.html index.htm;

}

}

server {

        listen       192.168.4.5:443;

        server_name  www.tarena.com;

ssl    on;                         //開啟SSL

ssl_certificate        cert.pem;   //指定證書文件 

ssl_certificate_key    cert.key;   //指定私鑰文件 

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers   on;

location /{

root   www;                        //指定網(wǎng)站根路徑

index  index.html index.htm;

}

}


2)生成私鑰與證書

[root@svr ~]# openssl genrsa -out cert.key 2048    //生成私鑰

[root@svr ~]# openssl req -new -x509 -key cert.key -out cert.pem  //生成證書

[root@svr ~]# cp {cert.key,cert.pem}/usr/local/nginx/conf


3)創(chuàng)建網(wǎng)站根目錄及對應首頁文件

[root@svr ~]# mkdir /usr/local/nginx/{www,bbs}

[root@svr ~]# echo “www” >/usr/local/nginx/www/index.html

[root@svr ~]# echo “bbs” >/usr/local/nginx/bbs/index.html


4)重啟nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx –s stop

[root@svr ~]# /usr/local/nginx/sbin/nginx



 客戶端測試

1)修改/etc/hosts文件,進行域名解析

[root@client ~]# vim /etc/hosts

192.168.4.5    www.tarena.com  bbs.tarena.com


2)登錄192.168.4.205主機進行測試

[root@client ~]# firefox http://bbs.tarena.com            //輸入密碼后可以訪問

[root@client ~]# firefox https://www.tarena.com            //信任證書后可以訪問


3)登錄非192.168.4.205的其他任意主機測試

[root@client ~]# firefox http://bbs.tarena.com            //無法訪問

[root@client ~]# firefox https://www.tarena.com            //信任證書后可以訪問Linux 搭建Nginx服務器


 

1.Nginx安裝

2.Nginx虛擬主機

3.Nginx用戶訪問控制、進行SSL加密


一、構建Nginx服務器


1)使用源碼包安裝nginx軟件包

[root@svr ~]# yum -y install zlib-devel  openssl-devel  pcre-devel  perl-devel   make  gcc       //安裝常見依賴包

[root@svr ~]# useradd –s /sbin/nologin nginx

[root@svr ~]# wget http://124.202.164.12/files/408500000A3B37E2/nginx.org/download/nginx-1.13.1.tar.gz

[root@svr ~]# tar  -zxvf   nginx-1.13.1.tar.gz

[root@svr ~]# cd  nginx-1.13.1

[root@svr nginx-1.13.1]# ./configure   \

>--prefix=/usr/local/nginx   \         //指定安裝路徑

>--user=nginx   \                     //指定用戶

>--group=nginx  \                    //指定組

>--with-http_stub_status_module  \  //開啟狀態(tài)統(tǒng)計功能

>--with-http_ssl_module            //開啟SSL加密功能


....

[root@svr nginx-1.13.1]# make && make install    //編譯并安裝


2)啟用nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx  //啟動Nginx


nginx服務默認通過TCP 80端口監(jiān)聽客戶端請求:

[root@svr ~]# netstat  -anptu  |  grep nginx

tcp        000.0.0.0:800.0.0.0:*        LISTEN        10441/nginx


3)為Nginx Web服務器建立測試首頁文件

Nginx Web服務默認首頁文檔存儲目錄為/usr/local/nginx/html/,在此目錄下建立一個名為index.html的文件:

[root@svr ~]# cat  /usr/local/nginx/html/index.html


<h2>Welcome to Nginx 192.168.4.5 </h2>



二、用戶認證及訪問控制




通過Nginx實現(xiàn)Web頁面的認證與訪問控制,需要修改Nginx配置文件,在location容器中添加allow及deny語句實現(xiàn)訪問控制,

添加auth語句實現(xiàn)用戶認證。最后使用htpasswd命令創(chuàng)建用戶及密碼即可。




 修改Nginx配置文件

1)修改/usr/local/nginx/conf/nginx.conf

[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf

....

server {

        listen       80;

        server_name  localhost;

        auth_basic "Input Password:";  //認證提示符

        auth_basic_user_file pass.txt;  //認證密碼文件

location /{

            root   html;

            index  index.html index.htm;

}

location /test {

                allow 192.168.4.205;   //訪問控制,僅192.168.4.205可以訪問

                deny all;    //拒絕所有

            index  index.html index.htm;

}

2)創(chuàng)建二級頁面目錄,并生成index.html文件

[root@svr ~]# mkdir /usr/local/nginx/html/test

[root@svr ~]# echo “test” >/usr/local/nginx/html/test/index.html


3)生成密碼文件,創(chuàng)建用戶tom及密碼


使用htpasswd命令創(chuàng)建賬戶文件,需要確保系統(tǒng)中已經(jīng)安裝了httpd-tools。

[root@svr ~]# htpasswd -cm /usr/local/nginx/conf/pass.txt tom

New password:

Re-type new password:

Adding password for user tom


4)重啟Nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx –s stop

[root@svr ~]# /usr/local/nginx/sbin/nginx


客戶端測試

1)登錄192.168.4.205主機進行測試

 http://192.168.4.5                    //輸入密碼后可以訪問

 http://192.168.4.5/test            //輸入密碼后可以訪問

 

2)登錄非192.168.4.205的其他任意主機測試

 http://192.168.4.5                //輸入密碼后可以訪問

 http://192.168.4.5/test        //輸入密碼后無法訪問

 

 

 

三、 Nginx虛擬主機



實現(xiàn)兩個基于域名的虛擬主機,域名分別為www.tarena.com和bbs.tarena.com

域名為bbs.tarena.com的Web服務僅允許192.168.4.205訪問


對域名為bbs.tarena.com的站點進行用戶認證,用戶名稱為tom,密碼為123456


對域名為www.tarena.com的站點進行SSL加密



修改Nginx配置文件,添加server容器實現(xiàn)虛擬主機功能;對于需要進行訪問控制的虛擬主機添加allow和deny語句;

對于需要進行用戶認證的虛擬主機添加auth認證語句;對于需要進行SSL加密處理的站點添加ssl相關指令。



1)修改Nginx服務配置,添加相關虛擬主機配置如下

[root@svr ~]# vim /usr/local/nginx/conf/nginx.conf

....

server {

        listen       192.168.4.5:80;  //端口

        server_name  bbs.tarena.com;  //域名

allow 192.168.4.205;           //僅192.168.4.205可以訪問

deny all;                      //拒絕所有 

auth_basic "Input Password:";  //認證提示符

        auth_basic_user_file pass.txt;  //認證密碼文件

location /{

            root   bbs;//指定網(wǎng)站根路徑

            index  index.html index.htm;

}

}

server {

        listen       192.168.4.5:443;

        server_name  www.tarena.com;

ssl    on;                         //開啟SSL

ssl_certificate        cert.pem;   //指定證書文件 

ssl_certificate_key    cert.key;   //指定私鑰文件 

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers   on;

location /{

root   www;                        //指定網(wǎng)站根路徑

index  index.html index.htm;

}

}


2)生成私鑰與證書

[root@svr ~]# openssl genrsa -out cert.key 2048    //生成私鑰

[root@svr ~]# openssl req -new -x509 -key cert.key -out cert.pem  //生成證書

[root@svr ~]# cp {cert.key,cert.pem}/usr/local/nginx/conf


3)創(chuàng)建網(wǎng)站根目錄及對應首頁文件

[root@svr ~]# mkdir /usr/local/nginx/{www,bbs}

[root@svr ~]# echo “www” >/usr/local/nginx/www/index.html

[root@svr ~]# echo “bbs” >/usr/local/nginx/bbs/index.html


4)重啟nginx服務

[root@svr ~]# /usr/local/nginx/sbin/nginx –s stop

[root@svr ~]# /usr/local/nginx/sbin/nginx



 客戶端測試

1)修改/etc/hosts文件,進行域名解析

[root@client ~]# vim /etc/hosts

192.168.4.5    www.tarena.com  bbs.tarena.com


2)登錄192.168.4.205主機進行測試

[root@client ~]# firefox http://bbs.tarena.com            //輸入密碼后可以訪問

[root@client ~]# firefox https://www.tarena.com            //信任證書后可以訪問


3)登錄非192.168.4.205的其他任意主機測試

[root@client ~]# firefox http://bbs.tarena.com            //無法訪問

[root@client ~]# firefox https://www.tarena.com            //信任證書后可以訪問


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI