zoukankan      html  css  js  c++  java
  • Linux 搭建Nginx并添加配置 SSL 证书

    1. 安装准备
     
    1.1 gcc安装
    安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
    [root@nginx ~]# yum -y install gcc-c++
    1.2 pcre安装
    PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
    [root@nginx ~]# yum -y install pcre pcre-devel
    1.3 zlib安装
    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
    [root@nginx ~]# yum -y install zlib zlib-devel
    1.4 OpenSSL安装
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
    [root@nginx ~]# yum -y install openssl openssl-devel
     
    2. Nginx安装
    2.1 Nginx版本
    下载网址:https://nginx.org/en/download.html
    选择最新的稳定版nginx-1.10.3
    版本说明:
    Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
    Stable version:最新稳定版,生产环境上建议使用的版本
    Legacy versions:遗留的老版本的稳定版
    2.2 Nginx下载
    使用wget命令下载
    1 [root@nginx ~]# wget -c https://nginx.org/download/nginx-1.10.3.tar.gz
    如没有wget命令则安装:
    [root@nginx ~]# yum -y install wget
    2.3 解压
    [root@nginx ~]# tar -zxvf nginx-1.10.3.tar.gz
     
    2.4.1 新建nginx用户和组 (根据自己的需求 可忽略此步)
    [root@nginx include]# groupadd nginx 
    [root@nginx include]# useradd -g nginx -d /home/nginx nginx 
    [root@nginx include]# passwd nginx
    2.4.2第三方模块安装
    本文以安装第三方模块sticky为例,版本为1.,2.5,
    Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route
    1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
    2.后端服务器处理完请求,将响应数据返回给nginx。
    3.此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
    4.客户端接收请求,并保存带route的cookie。
    5.当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。

    http {
      #OK include vhost/xxx.conf;
      upstream shop_server{
      sticky;
      #Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上
      # server 192.168.1.23; 
      server 192.168.1.24;
      # server 192.168.1.25;
      keepalive 32; 
    }
    上传解压:
    [root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz 
    [root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5
    2.4.3 安装
      先卸载老版本nginx(若为首次安装可忽略此步)

        查找并删除mysql有关的文件

        find / -name nginx

        rm -rf 上边查找到的路径,多个路径用空格隔开,或者下边一条命令即可 

        find / -name nginx|xargs rm -rf

      安装:
    [root@nginx ~]# cd nginx-1.10.3 
    [root@nginx nginx-1.10.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-1.2.5
    指定用户、路径和模块配置(可选):
    ./configure  
    --user=nginx --group=nginx  #安装的用户组 
    --prefix=/usr/local/nginx  #指定安装路径 
    --with-http_stub_status_module  #监控nginx状态,需在nginx.conf配置 
    --with-http_ssl_module  #支持HTTPS 
    --with-http_sub_module  #支持URL重定向 
    --with-http_gzip_static_module #静态压缩 
    --add-module=/usr/local/nginx-sticky-1.2.5 #安装sticky模块
    2.5 编译
    [root@nginx nginx-1.10.3]# make && make install
    报错:
    ];
     
    解决方法:
    修改ngx_http_sticky_misc.c文件,新增#include <openssl/sha.h>和#include <openssl/md5.h>模块
    [root@nginx nginx-1.10.3]# sed -i '12a #include <openssl/sha.h>' /usr/local/nginx-sticky-1.2.5/ngx_http_sticky_misc.c 
    [root@nginx nginx-1.10.3]# sed -i '12a #include <openssl/md5.h>' /usr/local/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
    重新编译:
    [root@nginx nginx-1.10.3]# make && make install
    3.6 nginx命令全局执行设置
    [root@nginx bin]# cd /usr/local/nginx/sbin/ 
    [root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
     
    4. Nginx相关命令
    4.1 版本查看
    [root@nginx ~]# nginx -v 

       nginx version: nginx/1.10.3

    4.2 查看加载的模块
    [root@nginx ~]# nginx -V 

       nginx version: nginx/1.10.3

      built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

      configure arguments: --add-module=/root/nginx-sticky-1.2.5/

    4.3 启停命令
    4.3.1 启动
    [root@nginx nginx-1.10.3]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    4.3.2 停止
    [root@nginx nginx-1.12.2]# nginx -s stop 
    [root@nginx nginx-1.12.2]# nginx -s quit
    4.3.3 动态加载
    [root@nginx nginx-1.12.2]# nginx -s reload
    4.3.4 测试配置文件nginx.conf正确性
    [root@nginx ~]# nginx -t 
      nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
      nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
     
    nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
    nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
    nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。
    4.4 开机自启动
    编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
    1 [root@nginx rc.d]# cd /etc/rc.d 
    2 [root@nginx rc.d]# sed -i '13a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local 
    3 [root@nginx rc.d]# chmod u+x rc.local
     
    5. 更改默认端口
    编辑配置文件/usr/local/nginx/conf/nginx.conf,将默认端口80修改为81:
    1 [root@nginx ~]# view /usr/local/nginx/conf/nginx.conf

     

    加载配置:
    1 [root@nginx ~]# nginx -s reload
     
    6. 访问Nginx
    6.1 关闭防火墙
    [root@nginx ~]# firewall-cmd --state 
    running
    [root@nginx ~]# systemctl stop firewalld.service
    [root@nginx ~]# firewall-cmd --state
    not running
    6.2 访问Nginx

     7.开始配置SSL证书(证书需另行获取,获取的途径很多,如阿里云的服务或第三方服务购买)

    进入 conf/nginx.conf 开始配置文件的修改。在修改配置文件之前,最好做一个备份,防止修改错误,也能及时回退错误
     
    7.1、找到第一个监听80端口的server:一下是我修改好的server
     
     1 server {
     2     listen 80;
     3     server_name 需要访问的域名;
     4  
     5     rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把    http的域名请求转成https
     6  
     7     #charset koi8-r;
     8     #access_log logs/host.access.log main;
     9     location / {
    10         root html;
    11         index index.html index.htm;
    12        proxy_pass http://需要访问的域名; #因为这里还是80端口,所以保持http就可以了
    13  
    14     }
    15 } 
     
    在实际的配置文件中,最好把我上面的备注删除
     
    7.2、第一个server修改好了之后。那么就需要开始配置第二个server。拉到文件的底部。看到有一个https类型的server。而且已经全部被注释封上了,这是我们需要改的第二个server,如图:
    这里除了HTTPS server这行之外,其他的 # 删除,启动https模块
     
     1 # HTTPS server
     2 #
     3 server {
     4     listen 443 ssl;
     5     server_name 需要访问的域名,这里也不用加https;
     6  
     7     ssl on;
     8  
     9     ssl_certificate /usr/local/nginx/ssl/ssl.pem; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
    10     ssl_certificate_key /usr/local/nginx/ssl/ssl.key; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
    11  
    12  
    13     ssl_session_cache shared:SSL:1m;
    14     ssl_session_timeout 5m;
    15  
    16     ssl_ciphers HIGH:!aNULL:!MD5;
    17     ssl_prefer_server_ciphers on;
    18  
    19     location / {
    20         proxy_pass http://需要访问的域名:8080/;
    21     }
    22 }
    7.3、检验配置文件是否有错误
     
     [root@localhost sbin]# nginx -t
     
    如果nginx曾经安装过SSL模块,那么应该会显示以下界面:(如果已经显示配置成功,那么可以跳过这一步,直接重启nginx就可以了)
     
    可是大多数第一次安装https证书,都会报错,说缺少SSL模块,如下:
     
     
    7.4、这时候我们就可以先安装SSL模块:
    先确认2个位置:
    1)我的nginx是安装在了/usr/local/nginx/下
    2)我的nginx的源码包放在了/usr/local/nginx/nginx/nginx-1.8.0下。如果没有的话,重新下载你对应的nginx版本的源码包,找个目录解压
     
    3)目录切换到我们的源码包安装位置:
     
    cd /usr/local/nginx/nginx/nginx-1.8.0
     
    4)执行语句,重新安装ssl模块:
     
    ./configure  --with-http_ssl_module
     
    5)这时候应该就执行配置成功了。配置成功后,那么就需要编译我们的配置。(注意这里只能用make 而不要用make install,因为执行make install是覆盖安装的意思)
    运行:
     
    make
     
    等待执行完成后,我们需要把新编译的nginx模块替换原来的nginx。
     
    6)还是老规矩,先备份旧的nginx,执行语句(这里面复制的文件的路径需要根据你们安装的目录自行修改,如果和我安装的路径是一样的那么可以直接运行该语句):
     
      cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
     
      先停止nginx进程,然后
     
        cp objs/nginx /usr/local/nginx/sbin/nginx
     
    7)然后就是启动nginx。在启动之前,也可以在测试一次配置文件是否已经生效:
     
    #先切换到sbin目录
    cd /usr/local/nginx/sbin/
    #检测nginx的配置文件是否有错误
    ./niginx -t
     
    看到这样的,就是已经成功了
     
     
    最后启动nginx:
     
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
     
    因为刚才替换nginx模块的时候是把nginx进程都杀死了,所以要用上面的命令进行启动,而不能使用reload重启。
     
    nginx正常启动后,我们在访问我们的网站,发现https就已经配置好了:
     
    附上nginx配置文件
      1 #user  nobody;
      2 worker_processes  1;
      3 
      4 #error_log  logs/error.log;
      5 #error_log  logs/error.log  notice;
      6 #error_log  logs/error.log  info;
      7 
      8 #pid        logs/nginx.pid;
      9 
     10 
     11 events {
     12     worker_connections  1024;
     13 }
     14 
     15 
     16 http {
     17     include       mime.types;
     18     default_type  application/octet-stream;
     19 
     20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     21     #                  '$status $body_bytes_sent "$http_referer" '
     22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
     23 
     24     #access_log  logs/access.log  main;
     25 
     26     sendfile        on;
     27     #tcp_nopush     on;
     28 
     29     #keepalive_timeout  0;
     30     keepalive_timeout  65;
     31 
     32     #gzip  on;
     33 
     34     server {
     35         listen       80;
     36         server_name  test.feihe.com;
     37         #rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把http的域名请求转成https
     38 
     39         #charset koi8-r;
     40 
     41         #access_log  logs/host.access.log  main;
     42 
     43         location / {
     44             root   html/;
     45             index  index.html index.htm;
     46         }
     47 
     48 
     49         location /coa/ {
     50             proxy_pass http://localhost:8080;
     51         }
     52 
     53         location /front/ {
     54             proxy_pass http://localhost:8080;
     55         }
     56 
     57         #error_page  404              /404.html;
     58 
     59         # redirect server error pages to the static page /50x.html
     60         #
     61         error_page   500 502 503 504  /50x.html;
     62         location = /50x.html {
     63             root   html;
     64         }
     65 
     66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     67         #
     68         #location ~ .php$ {
     69         #    proxy_pass   http://127.0.0.1;
     70         #}
     71 
     72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     73         #
     74         #location ~ .php$ {
     75         #    root           html;
     76         #    fastcgi_pass   127.0.0.1:9000;
     77         #    fastcgi_index  index.php;
     78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     79         #    include        fastcgi_params;
     80         #}
     81 
     82         # deny access to .htaccess files, if Apache's document root
     83         # concurs with nginx's one
     84         #
     85         #location ~ /.ht {
     86         #    deny  all;
     87         #}
     88     }
     89 
     90 
     91     # another virtual host using mix of IP-, name-, and port-based configuration
     92     #
     93     #server {
     94     #    listen       8000;
     95     #    listen       somename:8080;
     96     #    server_name  somename  alias  another.alias;
     97 
     98     #    location / {
     99     #        root   html;
    100     #        index  index.html index.htm;
    101     #    }
    102     #}
    103 
    104 
    105     # HTTPS server         ssl_protocols  SSLv2 SSLv3 TLSv1;
    106     
    107     server {
    108         listen       443 ssl;
    109         server_name  test.feihe.com;
    110 
    111         ssl                  on;
    112         ssl_certificate      /usr/local/nginx/ssl/_.xx.com_bundle.crt;  #这里是ssl pem文件存放的绝对路径
    113         ssl_certificate_key  /usr/local/nginx/ssl/_.xx.com.key;  #这里是ssl key文件存放的绝对路径
    114 
    115         ssl_session_cache    shared:SSL:1m;
    116         ssl_session_timeout  5m;
    117 
    118 
    119         ssl_ciphers  HIGH:!aNULL:!MD5;
    120         ssl_prefer_server_ciphers   on;
    121 
    122         location / {
    123             root   html/;
    124             index  index.html index.htm;
    125         }
    126 
    127 
    128         location /coa/ {
    129             proxy_pass http://localhost:8080;
    130         }
    131 
    132         location /front/ {
    133             proxy_pass http://localhost:8080;
    134         }
    135     }
    136 
    137 }
  • 相关阅读:
    java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
    spring-session-data-redis依赖冲突问题
    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod
    图片上传后台服务报内存溢出 Out Of Memory Java heap space
    mysql 数据库密码忘记重置 进行远程连接
    打Jar包
    Type interface com.innovationV2.mapper.UserMapper is not known to the MapperRegistry
    关于java基础类型Integer String的clone()
    clion使用clang编译
    token & refresh token 机制总结
  • 原文地址:https://www.cnblogs.com/haw2106/p/10113471.html
Copyright © 2011-2022 走看看