Linux下nginx配置https協議訪問的方法_Linux

來源:互聯網
上載者:User

一、配置nginx支援https協議訪問,需要在編譯安裝nginx的時候添加相應的模組--with-http_ssl_module

查看nginx編譯參數:/usr/local/nginx/sbin/nginx -V

如下所示:

configure arguments: --prefix=/usr/local/nginx --with-google_perftools_module --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35

如果沒有--with-http_gzip_static_module這個參數,需要重新編輯nginx

二、防火牆開啟https協議預設連接埠443

vi /etc/sysconfig/iptables #編輯防火牆設定檔,添加以下代碼

-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

:wq! #儲存退出

service iptables restart #重啟防火牆

三、建立https認證

確保機器上安裝了openssl和openssl-devel

yum install openssl openssl-devel #CentOS使用yum命令安裝

mkdir /usr/local/nginx/conf/ssl #建立認證存放目錄

cd /usr/local/nginx/conf/ssl #進入目錄

建立伺服器私密金鑰:openssl genrsa -des3 -out server.key 1024 #根據提示輸入認證口令

建立簽章要求的認證(CSR):openssl req -new -key server.key -out server.csr #輸入上面設定的口令

#根據提示輸入相應的資訊

Country Name (2 letter code) [XX]:cn #國家

State or Province Name (full name) []:zhejiang #省份

Locality Name (eg, city) [Default City]:hangzhou #城市

Organization Name (eg, company) [Default Company Ltd]:osyunwei #公司

Organizational Unit Name (eg, section) []:sys #部門

Common Name (eg, your name or your server's hostname) []:osyunwei #主機名稱

Email Address []:[email protected] #郵箱

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:123456 #認證請求密鑰,CA讀取認證的時候需要輸入密碼

An optional company name []:osyunwei #公司名稱,CA讀取認證的時候需要輸入密碼

openssl rsa -in server.key -out server_nopassword.key #對key進行解密

openssl x509 -req -days 365 -in server.csr -signkey server_nopassword.key -out server.crt

#標記認證使用上述私密金鑰和CSR

四、修改nginx設定檔,載入ssl認證

vi /usr/local/nginx/conf/nginx.conf #編輯

listen 80;

listen 443;

ssl on;

ssl_certificate /usr/local/nginx/conf/ssl/server.crt;

ssl_certificate_key /usr/local/nginx/conf/ssl/server_nopassword.key;

fastcgi_param HTTPS $https if_not_empty; #有https協議時自動使用https,否則忽略這個參數。

:wq! #儲存退出

service nginx restart #重啟nginx

rewrite ^(.*) https://www.osyunwei.com$1 permanent; #可以把http協議重新導向到https上面

使用https協議開啟網址,如下圖所示:

至此,Linux下nginx配置https協議訪問完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.