NGINX HTTPS搭建及常见问题

风之舞 2023-05-30 11:02:29 137
nginx 
简介: NGINX HTTPS搭建及常见问题
1)查看 openssl 版本
openssl version -a  
1.0.1以上的版本支持 TLS1.2
1.1.1以上的版本支持 TLS1.3
2)升级 openssl
到官网下载新版
https://www.openssl.org/source/
例如下载到 /usr/local
升级 

cd /usr/local
tar zxvf openssl-1.1.1c.tar.gz  
cd openssl-1.1.1c 
./config --prefix=/usr/local/openssl  
make && make install  
mv /usr/bin/openssl \
    /usr/bin/openssl.OFF  
mv /usr/include/openssl \
    /usr/include/openssl.OFF  
ln -s \
    /usr/local/openssl/bin/openssl \
    /usr/bin/openssl  
ln -s \
    /usr/local/openssl/include/openssl \
    /usr/include/openssl  echo "/usr/local/openssl/lib"\
    >>/etc/ld.so.conf  
ldconfig -v   

验证
openssl version -a  
3)重新编译 nginx
升级OpenSSL之后,nginx需要重新编译,否则TLS还是旧版本的
下面是基本安装,如您需求更多,请自行调整
用到的软件

openssl

前面已经安装完了

pcre

注意:nginx 使用的版本为 PCRE,不支持 PCRE2,所以要选择 8.* 的版本。
下载地址
http://www.pcre.org/
例如下载到 /usr/local
cd /usr/local
tar -zxv -f pcre-8.43.tar.gz
cd pcre-8.43
./configure --prefix=/usr/local/pcre/
make && make install

zlib

下载地址 
http://www.zlib.net/
例如下载到 /usr/local
cd /usr/local
tar -zxv -f zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib/
make && make install
编译nginx

tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3

./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-openssl=/usr/local/openssl-1.1.1c \
--with-pcre=/usr/local/pcre-8.43 \
--with-zlib=/usr/local/zlib-1.2.11 \
--with-debug \
--with-pcre-jit \
--with-ipv6 \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-cc-opt='-g -O2 -Wp,-D_FORTIFY_SOURCE=2'

make && make install





server
  {
    listen       80;
    listen       443 ssl;
    server_name  ol.test.com;
    index index.shtml index.html index.php;
    root  /home/web/htdocs/test.com/ol.test.com/php/ol.test.com;

    ssl_certificate       /home/web/htdocs/test.com/ol.test.com/service_conf/nginx/ssl_cert/ol.pianwan.    com_bundle.crt;#证书路径
    ssl_certificate_key          /home/web/htdocs/test.com/ol.test.com/service_conf/nginx/ssl_cert/ol.pianwan.    com.key;#证书路径
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;#顺序不一致可能会出现版本过低的问题
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;


    #https 301 jump
    if ($scheme = http ) {
        rewrite ^(.*) https://ol.test.com$1 permanent;
    }
    location = /status/ {
        stub_status on;
        access_log   off;
    }
      rewrite ^/wap(.*)$ /wap/route.php?pathinfo=$1 break;
      rewrite ^/danji/wap(.*)$ /wap/route.php?pathinfo=$1 break;
    set $need_rewrite '';
    if (!-f $request_filename)
    {
      set $need_rewrite '1';
    }
    if ($request_uri ~ ^/status/$)
    {
      set $need_rewrite '';
    }
    if ($request_uri ~ ^/phpmyadmin_bak/$)
    {
      set $need_rewrite '';
    }
    if ($need_rewrite)
    {
      rewrite ^(.*)$ /route.php?pathinfo=$1 break;
    }

    location ~ .*\.(php|php5)?$
    {     
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }
    location ~ .*\.(js|css)?$
    {
      expires      1d;
    }    
     # error_page 404 http://ol.test.com/404.html;
    access_log  /home/service/nginx/logs/access_ol.pianwan.log access;
  }