原文地址: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

Nginx从搭建到配置支持HTTPS的更多相关文章

  1. nginx使用ssl模块配置支持HTTPS访问

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求: 做一个网站域名为 www.localhost.cn 要求通过htt ...

  2. nginx使用ssl模块配置支持HTTPS访问【解决ssl错误】

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求:做一个网站域名为 www.localhost.cn 要求通过http ...

  3. nginx使用ssl模块配置支持HTTPS访问,腾讯云申请免费证书

    开始我尝试用 let's encrypt 让http 变 https 官方:https://github.com/certbot/certbot 参考:https://www.cnblogs.com/ ...

  4. Nginx配置支持https协议-应用实践

    Nginx配置支持https协议-应用实践 https简介 HTTPS 是运行在 TLS/SSL 之上的 HTTP,与普通的 HTTP 相比,在数据传输的安全性上有很大的提升. TLS是传输层安全协议 ...

  5. vs2013 配置支持https的libcurl

    需求:在vs2013上配置支持https协议的libcurl. 环境:win7(64位),vs2013 一.安装openssl: 需要先安装nasm和ActivePerl. nasm需要手动配置环境变 ...

  6. windows Apache 环境下配置支持HTTPS的SSL证书

    windows Apache 环境下配置支持HTTPS的SSL证书 1.准备工作 1)在设置Apache + SSL之前, 需要做: 安装Apache, 下载安装Apache时请下载带有SSL版本的A ...

  7. nginx服务器搭建以及配置

    2019年第一篇博客,在新的一年祝大家新年快乐,技术更上一层楼. 今天在公司搞了好长时间的nginx服务器搭建,以及遇到的问题,总结一下,方便查询 这里使用的是百度云的服务器,CentOS7系统的 N ...

  8. Haproxy配置支持https获取用户IP地址

    global log 127.0.0.1 local0 chroot /var/lib/haproxy #chroot运行路径 pidfile /var/run/haproxy.pid #haprox ...

  9. windows Apache 配置支持HTTPS的SSL证书

    在设置Apache + SSL之前, 需要做: 安装Apache, 下载安装Apache时请下载带有ssl版本的Apache安装程序. 并且ssl需要的文件在如下的位置: [Apache安装目录]/m ...

随机推荐

  1. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

  2. echarts解决一些大屏图形配置方案汇总

    本文主要记录使用echarts解决各种大屏图形配置方案. 1.说在前面 去年经常使用echarts解决一些可视化大屏项目,一直想记录下使用经验,便于日后快速实现.正好最近在整理文档,顺道一起记录在博客 ...

  3. Mac安装Gradle eclipse安装buildship插件

    一直用的eclipse+mvn,现在需要导入别人的gradle项目,所以下载了gradle和在eclipse中安装了buildship插件. 一,mac下安装gradle 1,点击网页https:// ...

  4. 什么是SQL注入?什么是XSS攻击?什么是CSRF攻击?

    1. XSS(Cross Site Script,跨站脚本攻击) 是向网页中注入恶意脚本在用户浏览网页时在用户浏览器中执行恶意脚本的攻击方式. 1.1跨站脚本攻击分有两种形式: 反射型攻击(诱使用户点 ...

  5. javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误解决办法

    今天不用eclipse.myeclipse等开发工具,纯手写JSP页面(有点作死)时突然出现以前从来没遇到过的问题,报错如下: HTTP Status 500 - java.lang.NoClassD ...

  6. sparkpython

    http://blog.csdn.net/ydq1206/article/details/51922148

  7. js 时间特效

    http://example.com:1234/test.htm#part2:Hash的作用. http://www.cnblogs.com/Interkey/p/RunAsAdmin.html

  8. Lucene.net入门学习(结合盘古分词)(转载)

    作者:释迦苦僧  出处:http://www.cnblogs.com/woxpp/p/3972233.html  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显 ...

  9. Angular 5.x 学习笔记(2) - 生命周期钩子 - 暂时搁浅

    Angular 5.x Lifecycle Hooks Learn Note Angular 5.x 生命周期钩子学习笔记 标签(空格分隔): Angular Note on cnblogs.com ...

  10. IEEP-网络设计

    IEEP-网络设计 网络设计概述 网络设计概述 1.负责把网络规划阶段获得的客户需求运用技术手段予以规范化体现 2.网络设计一般遵循模块化指导方针,分模块进行设计 3.网络设计的输出成果必须是规范的. ...