Nginx 反向配置,SSL 证书配置

本贴最后更新于 1629 天前,其中的信息可能已经天翻地覆

Nginx 安装、配置

安装

CentOS 6/7 安装 Nginx
添加镜像源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm  

通过 yum search nginx 看看是否已经添加源成功。如果成功则执行下列命令安装 Nginx

sudo yum install -y nginx   

Ubuntu 16/18 安装 Nginx

sudo apt-get install nginx  

浏览器输入 http://IP 如果出现 Welcome to nginx ! 字样表示安装成功。

默认安装位置:/usr/local/nginx

image.png

Nginx 目录下文件夹含义:

conf:nginx 所有配置文件
conf.d:默认的主配置文件
sbin : 存放主程序
html : 默认的静态页面
logs : 日志
scgi_temp : 临时目录
uwsgi_temp : 临时目录

Nginx 重启、停止、启动

#重启  
service nginx restart  
  
#停止  
/usr/local/nginx/sbin/nginx -s stop  
service nginx stop  
  
#启动  
/usr/local/nginx/sbin/nginx  
service nginx start  
  
#重新加载配置文件  
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
  

Nginx 配置 Http

编辑 conf/nginx.con http 下添加 include /usr/local/nginx/conf.d/*.conf;

vim /usr/local/nginx/conf/nginx.conf  

image.png

接着到 conf.d 下,新建任意以 .conf 结尾的文件

cd /usr/local/nginx/conf.d/  
touch tomcat.conf  
vim tomcat.conf  

如果 cdcond.d 目录下提示 -bash: cd: /usr/local/nginx/conf.d/: No such file or directory,需要创建 conf.d 文件夹,再进到 cond.d 下。

mdkir -p /usr/local/nginx/conf.d/  

接下来配置 http 协议 80 端口

eg:博客地址:http://127.0.0.1:8080/blogs/
使用 Nginx 反向代理 8080 端口 blogs 项目

upstream blogs{  
 ip_hash;  #session共享  
 server 127.0.0.1:8080 max_fails=3 fail_timeout=600s;  
}  
server {  
 listen 80 backlog=65535;  
 server_tokens off;  
 server_name  127.0.0.1;  
 #access_log /gs/nginx-logs/teach.access.log;  
 #proxy_set_header Host $host:$server_port;  
 proxy_set_header Host $host;  
 # 获取真实IP  
 proxy_set_header X-Real-IP $remote_addr;  
 proxy_set_header REMOTE-HOST $remote_addr;  
 # 获取代理者的真实ip  
 proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;  
 proxy_http_version 1.1;  
 #proxy_set_header Connection "";  
 proxy_buffering off;  
 proxy_redirect off;  
 location / {  
 #这两个方法都可,转发地址栏显示http://127.0.0.1,重定向显示http://127.0.0.1/blogs  
#        rewrite "/" http://127.0.0.1/blogs; #重定向  
 proxy_pass http://mblogs; #转发  
 }  
 location /blogs {  
 proxy_next_upstream http_502 http_504 error timeout invalid_header;  
 set $origin '*';#写入需要跨域的请求地址  
 if ($request_method = 'OPTIONS') {  
 add_header 'Access-Control-Allow-Origin' $origin;  
 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';  
 add_header 'Access-Control-Max-Age' 17280000;  
 #add_header 'Content-Type' 'multipart/form-data';  
 #add_header 'Content-Type' 'text/plain charset=UTF-8';  
 add_header 'Content-Length' 0;  
 return 204;  
 }  
 if ($request_method = 'POST') {  
 add_header 'Access-Control-Allow-Origin' $origin;  
 #add_header 'Content-Type' 'multipart/form-data';  
 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';  
 add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
 }  
 if ($request_method = 'GET') {  
 add_header 'Access-Control-Allow-Origin' $origin;  
 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';  
 add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
 }  
 proxy_pass http://blogs;  
 }  
 }  

Http 协议访问

http://127.0.0.1

至此,http 协议的反向代理就配置完成,下面是配置 https 协议。

申请 ssl 证书(阿里云为例)

阿里云传送带
腾讯云传送带

购买后在阿里云 SSL 证书管理控制台可以看到一个证书,未签发状态需要点击证书申请,填写绑定的域名和个人信息后,点击下一步等待申请,差不多不到一天就可以通过。

image.png
image.png
image.png
证书申请通过后,显示如下图;点击下载会弹出几大 Web 应用服务器的证书类型;点击下载即可,详情
image.png
image.png

配置 SSL

准备工作

将下载的 SSL 证书上传到服务器上面到 nginx 的根目录 /usr/local/nginx/

cd /usr/local/nginx/
#创建crt文件夹
mkdir crt
#剪切到crt目录下
mv 3006543_sll.cn_tomcat.zip crt/

# 安装unzip  gzip  如果已安装,跳过这一步
yum install unzip  gzip   #Centos安装
apt-get yum install unzip  gzip   #Ubuntu 安装

#解压
unzip 3006543_sll.cn_tomcat.zip

解压完成后可以看到 .pem.key 结尾的文件
.pem 是 SSL 的证书
.key 是 SSL 的秘钥

Nginx 配置 Https

进入到 conf.d 目录下

cd /usr/local/nginx/conf.d
#编辑配置文件tomcat.conf
vim tomcat.conf

完整文件如下:
注意修改 ssl_certificatessl_certificate_key 证书的路径和证书名字,127.0.0.1 改为你的域名

upstream blogs{
   ip_hash;
   server 127.0.0.1:8080 max_fails=3 fail_timeout=600s;
}
server {
        listen 80 backlog=65535;
        server_tokens off;
        server_name 127.0.0.1;
        #access_log /gs/nginx-logs/teach.access.log;
        #proxy_set_header Host $host:$server_port;
        proxy_set_header Host $host;
        # 获取真实IP
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        # 获取代理者的真实ip
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        #proxy_set_header Connection "";
        proxy_buffering off;
        proxy_redirect off;
     location / {
        rewrite "/" https://127.0.01;
     }
 }

server {
        listen 443;
        #ssl on;
        server_tokens off;
        server_name 127.0.0.1;
        #access_log /gs/nginx-logs/teach.access.log;
        #proxy_set_header Host $host:$server_port;
        proxy_set_header Host $host;
        # 获取真实IP
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        # 获取代理者的真实ip
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        #proxy_set_header Connection "";
        proxy_buffering off;
#        proxy_redirect off;
        ssl_certificate /usr/local/nginx/crt/a.pem;#证书文件名称
        ssl_certificate_key /usr/local/nginx/crt/a.key;#私钥文件名称
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #请按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#请按照这个套件配置
        ssl_prefer_server_ciphers on;
     location / {
        proxy_pass http://blogs;
     }
     location /blogs {
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
              set $origin '*';#写入需要跨域的请求地址
             if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' $origin;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Max-Age' 17280000;
                #add_header 'Content-Type' 'multipart/form-data';
                #add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
                }
             if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' $origin;
                #add_header 'Content-Type' 'multipart/form-data';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                }
             if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' $origin;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                }
             proxy_pass http://blogs;
     }
}

配置完成后重启 Nnginx

#重启  

/usr/local/nginx/sbin/nginx -s stop   
/usr/local/nginx/sbin/nginx
#或者
service nginx restart  

服务器需要关闭防火墙,云服务器需要在控制台开放 80,443 端口

Htpps 访问

https://127.0.0.1

  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 36 关注
  • 反向代理
    9 引用 • 38 回帖
  • SSL

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS 与 SSL 在传输层对网络连接进行加密。

    69 引用 • 190 回帖 • 492 关注
  • HTTPS
    98 引用 • 271 回帖 • 3 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...