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,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
人工智能 Ubuntu 前端开发
Dify部署全栈指南:AI从Ubuntu配置到HTTPS自动化的10倍秘籍
本文档介绍如何部署Dify后端服务及前端界面,涵盖系统环境要求、依赖安装、代码拉取、环境变量配置、服务启动、数据库管理及常见问题解决方案,适用于开发与生产环境部署。
1539 1
|
8月前
|
Ubuntu Linux 网络安全
Linux服务器之Ubuntu的安装与配置
Ubuntu Desktop是目前最成功、最流行的图形界面的Linux发行版;而Ubuntu Server也在服务器端市场占据了较大的份额。今天为大家详细介绍了Ubuntu Server的安装与配置,希望对你能有所帮助。关于VMware、VirtualBox等虚拟化软件的使用,朱哥还会在后续的文章中为大家详细介绍,敬请关注!
|
6月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
750 11
|
6月前
|
网络安全 开发工具 git
在GitLab CI中同步HTTPS仓库地址的yaml配置
最后,提交并推送 `.gitlab-ci.yml`文件到您的GitLab仓库。GitLab CI/CD将自动识别这个文件,并在每次推送到 `master`分支时执行定义的同步任务。
303 16
|
9月前
|
Kubernetes Linux 网络安全
Rocky Linux 8.9配置Kubernetes集群详解,适用于CentOS环境
初始化成功后,记录下显示的 `kubeadm join`命令。
623 0
|
10月前
|
Linux 网络安全 开发工具
在Linux下配置gitee与Github的远程仓库
注意,git push后,是输入你的账号与密码。这个步骤可以通过特殊设置省去,但是一开始还是不要太省。
517 0
|
前端开发 应用服务中间件 nginx
nginx部署之https访问按照不同路径访问不同项目
上一篇介绍过如何在一个nginx上使用同一个端口部署多个web项目进行访问.具体内容可以参考:nginx支持一个端口访问多个前端项目(http以及https).
|
Web App开发 应用服务中间件 nginx
|
11月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
982 87
下一篇
开通oss服务