0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Macのnginxローカル環境を自己証明書でSSL化した

Posted at

はじめに

の続きで、自己証明書でSSL化したのでメモしておきます。

SSL証明書の作成

注意
homebrewでnginxをインストールした場合のパスは、
/usr/local/etcではありません!

mkdir -p /opt/homebrew/etc/nginx/ssl
cd /opt/homebrew/etc/nginx/ssl

openssl genpkey -algorithm RSA -out localhost.key
openssl req -new -key localhost.key -out localhost.csr
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Shibuya
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany
Organizational Unit Name (eg, section) []:Development
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:your-email@example.com

途中で以下のような情報を入力するよう求められますが、Common Name (CN) に今回はlocalhostと入力します。
続きのコマンドも下記実行。

openssl x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt

nginx 設定 (nginx.conf) の編集

sudo vim /opt/homebrew/etc/nginx/nginx.conf
nginx.conf
server {
    listen       80;
    server_name  localhost;
    return 301 https://$host$request_uri;
    }

server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate /opt/homebrew/etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /opt/homebrew/etc/nginx/ssl/localhost.key;

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

nginx をリロード

sudo nginx -t  # 設定の確認
sudo brew services restart nginx

⚠️注意点

自己証明書は正式な証明書でないため、ブラウザでは保護されていない通信と出てくる点に注意してください。

スクリーンショット 2025-01-19 17.58.13.png

おわりに

また私の場合80ポートがなぜか競合していて、再起動ができず躓きました💦
プロセスを停止することで再起動できたので書いておきます↓

sudo kill -9 21700 22366
sudo brew services restart nginx

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?