Nginx從搭建到配置支持HTTPS的方法
安裝
基礎(chǔ)包
ububtu apt-get install build-essential apt-get install libtool centos yum -y install gcc automake autoconf libtool make yum install gcc gcc-c++
進入安裝目錄
cd /usr/local/src
安裝 PCRE 支持正則表達 使 Nginx 支持 Rewrite 功能
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz tar -zxvf pcre-8.42.tar.gz cd pcre-8.42 ./configure make make install
安裝 zlib 支持?jǐn)?shù)據(jù)壓縮
wget http://zlib.net/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make make install
安裝 openssl 支持 https
wget https://www.openssl.org/source/openssl-1.1.1-pre7.tar.gz tar -zxvf openssl-1.1.1-pre7.tar.gz cd openssl-1.1.1-pre7 ./configure make make install
Nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz tar -zxvf nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install
配置
配置文件地址
/usr/local/nginx/conf/nginx.conf
使用
命令
/usr/local/nginx/sbin/nginx # 啟動 Nginx /usr/local/nginx/sbin/nginx -t # 檢查 Nginx 配置文件正確性 /usr/local/nginx/sbin/nginx -s reload # 重新載入配置文件 /usr/local/nginx/sbin/nginx -s reopen # 重啟 Nginx /usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
進程關(guān)閉
# 查看進程號 ps -ef|grep nginx # 正常退出 kill -QUIT 進程號 # 快速停止 kill -TERM 進程號 kill -INT 進程號 # 強制退出 kill -KILL nginx 生成 cer 證書支持 https 生成 cer 證書 # 進入存放證書的目錄 /usr/local/nginx/conf/ssl # 創(chuàng)建服務(wù)器證書密鑰文件 server.key 私鑰 openssl genrsa -des3 -out server.key 1024 # 輸入密碼,確認(rèn)密碼,后面會使用 # 創(chuàng)建簽名請求的證書(CSR) openssl req -new -key server.key -out server.csr # 輸出內(nèi)容為: # Enter pass phrase for root.key: ← 輸入前面創(chuàng)建的密碼 # Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN # State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 # Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 # Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 # Organizational Unit Name (eg, section) []: ← 可以不輸入 # Common Name (eg, YOUR name) []: ← 此時不輸入 # Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填 # Please enter the following ‘extra' attributes # to be sent with your certificate request # A challenge password []: ← 可以不輸入 # An optional company name []: ← 可以不輸入 # 備份服務(wù)器密鑰文件 cp server.key server.key.org # 去除文件口令,生成公鑰 openssl rsa -in server.key.org -out server.key # Enter pass phrase for server.key.org: ← 輸入前面創(chuàng)建的密碼 # 生成證書文件 server.crt openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
配置 https
# /usr/local/nginx/conf/nginx.conf # # HTTPS server configuration # server { listen 443 ssl; # ssl 端口 server_name www.xingkongbj.com xingkongbj.com; # 域名 ssl on; # 開啟 ssl ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; location / { proxy_redirect off; # 禁止跳轉(zhuǎn) proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://98.142.138.177/; } } # nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf # 原因是nginx缺少http_ssl_module模塊,編譯安裝時帶上--with-http_ssl_module配置就可以了 # 切換到nginx源碼包 cd cd /usr/local/src/nginx-1.14.0/ # 查看 ngixn 原有的模塊 /usr/local/nginx/sbin/nginx -V # 重新配置 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module # 重新編譯,不需要 make install 安裝。否則會覆蓋 make # 備份原有已經(jīng)安裝好的 nginx cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx.conf # 將剛剛編譯好的 nginx 覆蓋掉原來的 nginx(ngixn必須停止) cp ./objs/nginx /usr/local/nginx/sbin/ # 這時,會提示是否覆蓋,請輸入yes,直接回車默認(rèn)不覆蓋 # 啟動 nginx,查看 nginx 模塊,發(fā)現(xiàn)已經(jīng)添加 /usr/local/nginx/sbin/nginx -V /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx
總結(jié)
以上所述是小編給大家介紹的Nginx從搭建到配置支持HTTPS的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Nginx+SpringBoot實現(xiàn)負(fù)載均衡的示例
這篇文章主要介紹了Nginx優(yōu)雅的實現(xiàn)負(fù)載均衡,幫助大家更好的理解和使用nginx,感興趣的朋友可以了解下2020-10-10nginx反向代理之將80端口請求轉(zhuǎn)發(fā)到8080
這篇文章主要介紹了nginx反向代理之將80端口請求轉(zhuǎn)發(fā)到8080,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08