Nginx服務器的搭建及配置

Linux 搭建Nginx服務 html

1.Nginx安裝nginx

2.Nginx虛擬主機vim

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


1、構建Nginx服務器session


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

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

[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  \  //開啓狀態統計功能

>--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端口監聽客戶端請求:

[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


<h1>Welcome to Nginx 192.168.4.5 </h1>



2、用戶認證及訪問控制




經過Nginx實現Web頁面的認證與訪問控制,須要修改Nginx配置文件,在location容器中添加allow及deny語句實現訪問控制,

添加auth語句實現用戶認證。最後使用htpasswd命令建立用戶及密碼便可。




 修改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)建立二級頁面目錄,並生成index.html文件

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

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


3)生成密碼文件,建立用戶tom及密碼


使用htpasswd命令建立帳戶文件,須要確保系統中已經安裝了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        //輸入密碼後沒法訪問

 

 

 

3、 Nginx虛擬主機



實現兩個基於域名的虛擬主機,域名分別爲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容器實現虛擬主機功能;對於須要進行訪問控制的虛擬主機添加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;//指定網站根路徑

            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;                        //指定網站根路徑

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)建立網站根目錄及對應首頁文件

[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加密


1、構建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  \  //開啓狀態統計功能

>--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端口監聽客戶端請求:

[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


<h1>Welcome to Nginx 192.168.4.5 </h1>



2、用戶認證及訪問控制




經過Nginx實現Web頁面的認證與訪問控制,須要修改Nginx配置文件,在location容器中添加allow及deny語句實現訪問控制,

添加auth語句實現用戶認證。最後使用htpasswd命令建立用戶及密碼便可。




 修改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)建立二級頁面目錄,並生成index.html文件

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

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


3)生成密碼文件,建立用戶tom及密碼


使用htpasswd命令建立帳戶文件,須要確保系統中已經安裝了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        //輸入密碼後沒法訪問

 

 

 

3、 Nginx虛擬主機



實現兩個基於域名的虛擬主機,域名分別爲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容器實現虛擬主機功能;對於須要進行訪問控制的虛擬主機添加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;//指定網站根路徑

            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;                        //指定網站根路徑

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)建立網站根目錄及對應首頁文件

[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            //信任證書後能夠訪問

相關文章
相關標籤/搜索