8月22 centos安装Nginx 配置https,配置转发

一、centos安装Nginx

一. gcc 安装 
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: 
yum install gcc-c++ 
二. PCRE pcre-devel 安装 
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。 
nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令: 
yum install -y pcre pcre-devel

三. zlib 安装 
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。 
yum install -y zlib zlib-devel 
四. OpenSSL 安装 
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议。 
并提供丰富的应用程序供测试或其它目的使用。 
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。 
yum install -y openssl openssl-deve

将nginx-1.8.0.tar.gz拷贝至linux服务器。

解压: 
tar -zxvf nginx-1.8.0.tar.gz 
cd nginx-1.8.0

1、 configure 
./configure –help查询详细参数(参考本教程附录部分:nginx编译参数)

参数设置如下: 
./configure  
–prefix=/usr/local/nginx  
–pid-path=/var/run/nginx/nginx.pid  
–lock-path=/var/lock/nginx.lock  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log  
–with-http_gzip_static_module  
–http-client-body-temp-path=/var/temp/nginx/client  
–http-proxy-temp-path=/var/temp/nginx/proxy  
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi  
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi  
–http-scgi-temp-path=/var/temp/nginx/scgi

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

上面这一步可以不配置

./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
(注: --with-http_ssl_module:这个不加后面在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常)
$ make && make install

--with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源码路径。--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。

启动:

$ /usr/local/nginx/sbin/nginx

重启

$ /usr/local/nginx/sbin/nginx –s reload

停止:
$ /usr/local/nginx/sbin/nginx –s stop

测试配置文件是否正常:

 $ /usr/local/nginx/sbin/nginx –t

参考:https://blog.csdn.net/wxyjuly/article/details/79443432

https://blog.csdn.net/newlifely/article/details/73485840

二、证书

参考:https://www.itrus.cn/service_view_81.html

二、安装服务器证书

1.获取服务器证书文件 
将证书签发邮件中的包含服务器证书代码的文本复制出来(包括“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”)粘贴到记事本等文本编辑器中。 
为保障服务器证书在客户端的兼容性,服务器证书需要安装两张中级CA证书(不同品牌证书,可能只有一张中级证书)。 
在服务器证书代码文本结尾,回车换行不留空行,并分别粘贴两张中级CA证书代码(包括“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”,每串证书代码之间均需要使用回车换行不留空行),修改文件扩展名,保存包含三段证书代码的文本文件为server.pem文件(如果只有一张中级证书,则只需要粘贴一张中级证书代码与服务器证书代码即可,并回车换行)。 

2.安装服务器证书
复制server.key、server.pem文件到Nginx安装目录下的conf目录。
打开Nginx安装目录下conf目录中的nginx.conf文件 
找到 
    # HTTPS server 
    # 
    #server { 
    #    listen       443; 
    #    server_name  localhost; 
    #    ssl                  on; 
    #    ssl_certificate      cert.pem; 
    #    ssl_certificate_key  cert.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 / { 
    #        root   html; 
    #        index  index.html index.htm; 
    #    } 
    #} 
将其修改为 
    server { 
        listen       443; 
        server_name  localhost; 
        ssl                  on; 
        ssl_certificate      server.pem; 
        ssl_certificate_key  server.key; 
        ssl_session_timeout  5m; 
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
#启用TLS1.1、TLS1.2要求OpenSSL1.0.1及以上版本,若您的OpenSSL版本低于要求,请使用 ssl_protocols TLSv1;
        ssl_ciphers  HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; 
        ssl_prefer_server_ciphers   on; 
        location / { 
            root   html; 
            index  index.html index.htm; 
        } 
    } 
保存退出,并重启Nginx。 

证书检测,https://www.chinassl.net/ssltools/ssl-checker.html

三、https 转发配置

    # HTTPS server
    server {
        listen       81 ssl;
        server_name  loreal;
        ssl  on;
        ssl_certificate      /home/azureuser/sslkey/server.pem;
        ssl_certificate_key  /home/azureuser/sslkey/server.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /gateway9093/ {
           rewrite ^.+gateway9093/?(.*)$ /$1 break;
           proxy_pass http://xx.xx.160:9093/;
           proxy_set_header X-Real-IP $remote_addr;
        }
    }

注意证书绑定的是域名

访问

https://xxx:81/gateway9093/swagger-ui.html

会跳转到xx:9093/swagger-ui.html

原文地址:https://www.cnblogs.com/lyon91/p/9515808.html