小知识:Linux下nginx配置https协议访问的方法

一、配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块–with-http_ssl_module

查看nginx编译参数:/usr/local/nginx/sbin/nginx -v

%小知识:Linux下nginx配置https协议访问的方法-猿站网-插图

如下所示:

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 #根据提示输入证书口令

%小知识:Linux下nginx配置https协议访问的方法-1猿站网-插图

创建签名请求的证书(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 servers 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

%小知识:Linux下nginx配置https协议访问的方法-2猿站网-插图

四、修改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协议访问的方法-3猿站网-插图

至此,linux下nginx配置https协议访问完成。

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:自制Linux终端锁屏工具

2023-4-21 5:32:21

建站知识

小知识:Linux Shell里面生成随机数的一些思路分析

2023-4-21 5:49:01

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索