Nginx部署https網站并配置地址重寫的方法

1.環(huán)境準備:centos6.9主機一臺,關閉防火墻和selinux

安裝依賴包:yum -y install openssl-devel? pcre-devel gcc

創(chuàng)建nginx用戶:

useradd?-m?-s?/sbin/nologin?nginx?#不為nginx用戶創(chuàng)建家目錄,沒有可交互的shell tar?-xf?nginx-1.8.0.tar.gz cd?nginx-1.8.0 ./configure?--user=nginx?--group=nginx?--prefix=/usr/local/nginx?--with-http_stub_status_module? ?--with-http_ssl_module (--with-http_ssl_module指定安裝安全模塊,部署https網站時必須要安裝,/usr/local/nginx目錄不需要事先創(chuàng)建) make?&&?make?install cd?/usr/local/nginx ls?/usr/local/nginx

Nginx部署https網站并配置地址重寫的方法

conf?#存放nginx配置文件 logs?#存放服務日志和pid文件 html?#存放網站頁面的 sbin?#可執(zhí)行主程序目錄

2.將啟動程序做鏈接放到/usr/sbin/路徑下:

ln -s /usr/local/nginx/sbin/nginx? ?/usr/sbin/

nginx命令:

nginx????#?啟動服務? nginx?-s?stop??#關閉服務 nginx?-s?reload?#重新加載配置文件 nginx?-t???#測試配置文件 nginx?-v????#查看版本信息 nginx?-v????#查看編譯選項

啟動服務:nginx

Nginx部署https網站并配置地址重寫的方法

通過ip測試網站是否可以正常訪問,出現如下頁面說明網站配置成功(此時只有一個默認http網頁):

Nginx部署https網站并配置地址重寫的方法

3.使用openssl生成證書,配置https網站:

cd?/usr/local/nginx/conf  openssl?genrsa?-out?my.key????????#生成rsa算法的私鑰

Nginx部署https網站并配置地址重寫的方法

openssl?req?-new?-x509?-key?my.key?-out?my.crt?#生成子簽名證書,相當于公鑰

Nginx部署https網站并配置地址重寫的方法

修改nginx配置文件,指定證書所在的位置:

vim?/usr/local/nginx/conf/nginx.conf ...?... server?{ ?listen?443?ssl; ?server_name?www.test.com; ?ssl_certificate?my.crt;????#指定證書位置,默認在當前目錄尋找 ?ssl_certificate_key?my.key;??#指定私鑰位置 ?location?/?{? ??root?/var/www/html;???#指定網頁文件根路徑,與http網站路徑分開,便于區(qū)分 ??index?index.html; ??} }

修改完成后重加加載配置文件:nginx -s reload

mkdir?-p?/var/www/html echo?"ssl?test"?>/var/www/html/index.html

Nginx部署https網站并配置地址重寫的方法

4.進行訪問驗證:

http訪問效果如下:

Nginx部署https網站并配置地址重寫的方法

https訪問效果如下:

Nginx部署https網站并配置地址重寫的方法

5.配置http地址重寫,使客戶端訪問http時自動跳轉到https:

vim?/usr/local/nginx/conf/nginx.conf ...?... ?server?{ ??listen??80; ??server_name?www.test.com; ??rewrite?^(.*)$?https://${server_name}$1?permanent;??#接收到http訪問請求時,重定向到https ??location?/?{ ???root?html; ???index?index.html?index.htm; ??}

修改完成,重新加載配置文件:

nginx? -s? reload

6.再次訪問進行驗證:

通過http協(xié)議訪問網頁時,會自動跳轉到https:

Nginx部署https網站并配置地址重寫的方法

如果域名未做解析,請?zhí)砑觝osts記錄,將域名和ip對應關系寫到hosts文件中。

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享