ホームページ  >  記事  >  運用・保守  >  Nginx で https をアップグレードする方法

Nginx で https をアップグレードする方法

WBOY
WBOY転載
2023-05-14 16:49:12948ブラウズ

証明書のダウンロード

証明書コンソールで nginx バージョンの証明書をダウンロードします。解凍後にローカルエリアにダウンロードされた圧縮ファイルパッケージには、以下が含まれます。

  • .pem ファイル: 証明書ファイル

  • .key ファイル: 秘密鍵証明書のファイル (証明書の申請時に csr を自動的に作成することを選択しなかった場合、そのようなファイルは存在しません)

nginx
の構成

1. nginx の場合 インストールディレクトリに cert ディレクトリを作成し、ダウンロードしたファイルをすべて cert ディレクトリにコピーします 証明書申請時に csr ファイルを自分で作成する場合は、対応する秘密鍵ファイルを配置してください証明書ディレクトリ内。

2. nginx インストール ディレクトリの conf ディレクトリにある nginx.conf ファイルを開きます

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;

events {
  worker_connections 1024;
}

http {
  include    mime.types;
  default_type application/octet-stream;

  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  gzip on;  #开启gzip
  gzip_min_length 1k; #低于1kb的资源不压缩
  gzip_comp_level 3; #压缩级别【1-9】,越大压缩率越高,同时消耗cpu资源也越多,建议设置在4左右。
  gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; #需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片,下面会讲为什么。
  gzip_disable "msie [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
  gzip_vary on; #是否添加“vary: accept-encoding”响应头

  server {
    listen    80 default backlog=2048; #配置http可用
    listen    443 ssl; #配置https
    server_name localhost;

    ssl_certificate   ../cert/hzzly.pem; #配置证书文件
    ssl_certificate_key ../cert/hzzly.key; #配置私钥文件

    ssl_session_cache  shared:ssl:1m;
    ssl_session_timeout 5m;

    ssl_ciphers high:!anull:!md5;
    ssl_prefer_server_ciphers on;

    location / {
      root  /home/hzzly;
      index index.html index.htm;
    }

    # location ^~ /apis/ {
    #   proxy_set_header host $host;
    #   proxy_set_header x-real-ip $remote_addr;
    #   proxy_set_header x-forwarded-server $host;
    #   # 匹配任何以 /apis/ 开始的请求,并停止匹配 其它location
    #   proxy_pass http://xxxxxxxxxx/;
    # }

    # location ^~ /assets/ {
    #   gzip_static on;
    #   expires max;
    #   add_header cache-control public;
    # }
  }
}

3. nginx を再起動します

$ cd /usr/local/nginx/sbin
$ ./nginx -s reload

エラーの詳細

1. nginx で ssl モジュールが有効になっていない場合、https の設定時にエラーが表示されます。

nginx: [emerg] "ssl" パラメーターには ngx_http_ssl_module in ...

nginx が ssl モジュールを開きます

ソース パッケージに切り替えます:

$ cd /usr/local/src/nginx-1.16.0

新しい設定パラメータを変更します

$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

設定が完了したら、コマンド

$ make //这里不要进行make install,否则就是覆盖安装

を実行します。元のインストールされている nginx をバックアップします。

$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

元の nginx をコンパイルしたばかりの nginx

$ cp ./objs/nginx /usr/local/nginx/sbin/
## で上書きします。 #nginx

$ cd /usr/local/nginx/sbin
$ ./nginx -s reload
を再起動します

以上がNginx で https をアップグレードする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。