Nginx从搭建到配置支持HTTPS

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

    原文地址:https://www.xingkongbj.com/blog/nginx/nginx.html

    安装

    基础包

    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 支持数据压缩

    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

    进程关闭

    # 查看进程号
    ps -ef|grep nginx # 正常退出
    kill -QUIT 进程号 # 快速停止
    kill -TERM 进程号
    kill -INT 进程号 # 强制退出
    kill -KILL nginx

    生成 cer 证书支持 https

    生成 cer 证书

    # 进入存放证书的目录
    /usr/local/nginx/conf/ssl # 创建服务器证书密钥文件 server.key 私钥
    openssl genrsa -des3 -out server.key 1024
    # 输入密码,确认密码,后面会使用 # 创建签名请求的证书(CSR)
    openssl req -new -key server.key -out server.csr
    # 输出内容为:
    # Enter pass phrase for root.key: ← 输入前面创建的密码
    # 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 []: ← 可以不输入 # 备份服务器密钥文件
    cp server.key server.key.org # 去除文件口令,生成公钥
    openssl rsa -in server.key.org -out server.key
    # Enter pass phrase for server.key.org: ← 输入前面创建的密码 # 生成证书文件 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; # 禁止跳转
    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 # 备份原有已经安装好的 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,直接回车默认不覆盖 # 启动 nginx,查看 nginx 模块,发现已经添加
    /usr/local/nginx/sbin/nginx -V
    /usr/local/nginx/sbin/nginx -t
    /usr/local/nginx/sbin/nginx
  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
    标签: Nginx

    “Nginx从搭建到配置支持HTTPS” 的相关文章

    Python数据分析库介绍及引入惯例

    文章和代码等已经归档至【Github仓库:https://github.com/timerring/dive-into-AI 】或者公众号【AIShareLab】回复 python数据分析 也可获取。python的缺点Python有一个叫做全局解释器锁(Global Interpreter Lock...

    最全面的SpringBoot教程(三)——SpringBoot Web开发

    前言 本文为SpringBoot Web开发相关内容介绍下边将对静态资源管理包括静态资源访问静态资源前缀webjar首页支持请求参数处理包括Rest风格参数注释数据响应模板引擎包括Thymeleaf模板引擎基本语法thymeleaf使用登录功能 + 拦截器异常处理等进行详尽介绍~ 博...

    输出二维数组

    #include<iostream> using namespace std; int main() { int ia[3][4]={ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }...

    spring-boot-maven-plugin打包时排除provided依赖问题怎么解决 - 开发技术

    这篇文章主要介绍了spring-boot-maven-plugin打包时排除provided依赖问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇spring-boot-maven-plugin打包时排除provided依赖问题怎...

    mybatisPlus更新字段值为null怎么解决 - 开发技术

    这篇文章主要介绍“mybatisPlus更新字段值为null怎么解决”,在日常操作中,相信很多人在mybatisPlus更新字段值为null怎么解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mybatisPlus更新字段值为null...

    Java单例模式复习(三)

    心血来潮,在学学单例模式,以下是代码块:具备前面两种实现方式,package singleton; public class SingletonStatic { private SingletonStatic() { System.out.println("stati...