nginx本地配置https

Win10环境下Nginx配置本地HTTPS项目教程
本文详细介绍了在Windows 10系统中如何使用Nginx配置本地项目的HTTPS服务。首先通过Chocolatey安装所需工具,接着使用mkcert创建并安装本地可信证书,最后对Nginx配置文件进行编辑,包括设置443端口监听、SSL证书路径等,实现HTTPS强制跳转。

WIN10环境Nginx配置本地项目HTTPS

安装chocolatey

  1. 管理员身份打开cmd

  2. 在cmd窗口运行以下命令

   @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" 
  1. 验证是否安装成功(显示版本号则成功)

     choco 
    

安装mkcert

  1. 使用chocolatey安装

choco install mkcert
  1. 添加环境变量

    我的电脑->属性->高级系统设置->环境变量->系统变量->Path->新建->输入mkcert地址(mkcert.exe所在地址。具体看安装目录。)

    C:\ProgramData\chocolatey\lib\mkcert\tools
    
  2. 验证是否安装完成

     mkcert
    
  3. 使用mkcert安装本地系统信任

    mkcert -install
    
  4. 使用mkcert创建证书

    mkcert local.tp5.com(证书名称)
    

Nginx 配置(443端口及指定证书路径)

编辑nginx.conf

如果使用了nignx.conf 使用了include /etc/nginx/conf.d/*.conf; 则去修改相应的xxx.conf配置文件
server{
    listen 80; #监听80端口
    listen [::]:80;

    server_name  local.tp5.com; #域名

    root /www/tp5/public; #网站跟目录
    index index.html index.php index.htm;

    # return 301 https://$host/shengji.html;
    return 301 https://$host$request_uri; #强制跳转到https

    
    location /reader {
        if ($http_user_agent ~* "Baiduspider|360Spider|bingbot|Googlebot|Sogou web spider") {
            return 301 https://$host;
        }
    }
    #php设置
    location ~ \.php$ {
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+\.php)(/.+)") {
            set $script $1;
            set $path_info $2;
        }
        fastcgi_pass php73:9000;
        fastcgi_buffer_size 512k;
        fastcgi_buffers 6 512k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 512k;
        fastcgi_index index.php?IF_REWRITE=1;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $script;
        fastcgi_read_timeout 10000;
        include fastcgi_params;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css|txt)?$ {
        expires      12h;
    }

    location ~ /\. {
        deny all;
    }
}

server{
    listen 443 ssl;
    listen [::]:443;
    server_name  local.tp5.com;

    root /www/tp5/public;
    index index.html index.php index.htm;

    #return 301 https://$host/shengji.html;
    #return 301 https://$host$request_uri;
    ssl on;
    ssl_certificate /etc/nginx/conf.d/cert/local.tp5.com.pem;  #letsencrypt证书路径,
    ssl_certificate_key /etc/nginx/conf.d/cert/local.tp5.com-key.pem; #letsencrypt秘钥路径,
    

    location ~ \.php$ {
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+\.php)(/.+)") {
            set $script $1;
            set $path_info $2;
        }
        fastcgi_pass php73:9000;
        fastcgi_buffer_size 512k;
        fastcgi_buffers 6 512k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 512k;
        fastcgi_index index.php?IF_REWRITE=1;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $script;
        fastcgi_read_timeout 10000;
        include fastcgi_params;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css|txt)?$ {
        expires      12h;
    }

    location ~ /\. {
        deny all;
    }

    
}
### 配置本地IP使用Nginx支持HTTPS 要在本地IP上配置Nginx以支持HTTPS,需要完成以下几个方面的设置:生成SSL证书和密钥、配置Nginx以支持HTTPS,并确保Nginx能够正确加载这些配置。 #### 1. 生成SSL证书和密钥 为了使Nginx支持HTTPS,首先需要生成SSL证书和密钥。可以使用OpenSSL工具生成自签名证书。以下是生成证书的命令: ```bash sudo mkdir -p /etc/nginx/ssl sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \ -keyout /etc/nginx/ssl/nginx.key \ -out /etc/nginx/ssl/nginx.crt \ -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=IT/CN=localhost" ``` 上述命令会生成一个有效期为10年的自签名SSL证书[^4]。 #### 2. 配置Nginx支持HTTPS 接下来,编辑Nginx配置文件以支持HTTPS。假设本地IP为`127.0.0.1`,以下是一个示例配置: ```nginx server { listen 443 ssl; server_name 127.0.0.1; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; location / { proxy_send_timeout 600; proxy_read_timeout 600; proxy_connect_timeout 600; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8088/; add_header Access-Control-Allow-Origin *; } } ``` 在上述配置中: - `listen 443 ssl;` 表示Nginx监听443端口并启用SSL。 - `ssl_certificate` 和 `ssl_certificate_key` 指定了SSL证书和密钥的路径。 - `ssl_protocols` 和 `ssl_ciphers` 定义了支持的TLS协议版本和加密算法。 #### 3. 测试并重新加载Nginx配置 完成配置后,需要测试Nginx配置文件是否有语法错误,并重新加载配置文件以应用更改。运行以下命令: ```bash sudo nginx -t sudo nginx -s reload ``` 如果配置文件没有问题,Nginx将成功重新加载配置[^2]。 #### 4. 浏览器验证 打开浏览器,访问`https://127.0.0.1`。由于使用的是自签名证书,浏览器可能会提示安全警告。可以忽略警告并继续访问页面以验证配置是否成功[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值