Linux下nginx配置https协议访问

简介:

一、配置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 []:xxx@qq.com  #邮箱

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协议打开网址,如下图所示:











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1792318,如需转载请自行联系原作者
目录
相关文章
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
28 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
95 0
|
4天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
14 0
|
6天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
6天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
14 0
|
17天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
25 0
|
18天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
32 0
|
27天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0
|
28天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0
|
Linux 网络安全 数据安全/隐私保护