Nginx自签证书配置HTTPS协议访问

文章目录

    • 安装nginx
        • firewalld
    • 生成证书
      • 新建证书目录
      • 生成X509证书
      • 配置nginx配置文件
      • 访问测试

组件 版本
OS CentOS 7.5
nginx 1.14.1-9

安装nginx

yum install epel-release
yum install nginx
systemctl start nginx

firewalld

如果有防火墙策略的需求的话,没有的话跳过:

# 允许http和https服务
sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https
sudo firewall-cmd --runtime-to-permanent
# 允许 80(http),443(https) 端口
sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT

生成证书

新建证书目录

mkdir /etc/ssl/private
sudo chmod 700 /etc/ssl/private

生成X509证书

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/signed.key -out /etc/ssl/certs/signed.crt

其中:

  • signed.key: RSA私钥
  • signed.crt: 证书

参数解释:
req: openssl子命令,使用X.509是一项公钥基础设施行业标准,SSL/TLS遵从这个标准。
-keyout : 私钥输出路径
-out: 证书输出路径

然后命令执行后需要输入一些证书信息:

Country Name (2 letter code) [AU]: 直接回车
State or Province Name (full name) [Some-State]:直接回车
Locality Name (eg, city) []:直接回车
Organization Name (eg, company) [Internet Widgits Pty Ltd]:直接回车
Organizational Unit Name (eg, section) []:直接回车
Common Name (e.g. server FQDN or YOUR name) []:输入IP
Email Address []:[email protected]

配置nginx配置文件

编辑/etc/nginx/nginx.conf,我的文件是这样的:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  192.168.3.91;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  192.168.3.91;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/ssl/certs/signed.crt";
        ssl_certificate_key "/etc/ssl/private/signed.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

重启nginx: systemctl restart nginx

访问测试

一开始会提示不安全,直接详情->继续,就看到https页面可以访问。之后就不会提示了。
Nginx自签证书配置HTTPS协议访问_第1张图片

你可能感兴趣的:(nginx)