Nginx (Windows)

自己学习的记录,有很多不足的地方,查看需谨慎

步骤 1: 安装Chocolatey

  1. https://chocolatey.org并按照说明进行安装。
  2. 在PowerShell中运行以下命令:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  1. choco --version ===>choco upgrade chocolatey

步骤 2: 安装nginx

  1. choco install nginx
  2. net start nginx net stop nginx (启动/停止)
  3. Start-Service nginx ==> Stop-Service nginx ===> Restart-Service nginx (启动/停止/重启)

步骤 3: 生成自签名证书 (自测使用)443 ssl 配置https模式)

  1. 下载 OpenSSL 二进制文件:访问 https://slproweb.com/products/Win32OpenSSL.html 并下载适合你系统架构的安装包(32位或64位)。
  2. openssl version
  3. openssl genrsa -out server.key 2048
  4. openssl req -new -key server.key -out server.csr
  5. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  1. 这将生成一个名为 server.crt 的自签名证书文件,有效期为 365 天。
    现在,你有了一个自签名证书文件 server.crt 和对应的私钥文件 server.key。你可以将这些文件配置到你的 Nginx 服务器上使用。
  2. 请注意,自签名证书会有一些安全警告,因为它们不是由可信的第三方机构签发的。这意味着用户在访问使用自签名证书的网站时,可能会看到安全警告。对于生产环境,你应该使用由可信 CA 签发的证书。
  3. 在本地电脑host文件配置 127.0.1 ddy.com

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 优化文件传输
    sendfile        on;
    # 在 sendfile 启用时,尝试合并小的文件块以减少 TCP 数据包数量
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    # 开启资源压缩
    gzip  on;
    # 在头部中添加Vary: Accept-Encoding(建议开启)
    gzip_vary on;
    # 指定会被压缩的文件类型(也可自己配置其他类型)
    gzip_types text/plain application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png js/js;
    # 设置压缩级别,越高资源消耗越大,但压缩效果越好1-9
    gzip_comp_level 9;
    # 设置触发压缩的最小阈值
    gzip_min_length 50k;

    # 对于 ddy.com 的配置
    server {
        listen 80;
        server_name ddy.com;
        # 重新定向到
        return 301 https://$host$request_uri;
    }
    #HTTP HTTPS server
    server {
        #listen 80;
        listen 443 ssl;   #默认端口  可配8443  访问时 https://ddy.com:8443
        server_name ddy.com;
        ssl_certificate ../server.crt;
        ssl_certificate_key ../server.key;
        location / {
            try_files $uri $uri/ /index.html;
            root    C:/Users/11605/Desktop/dist/;
            index  index.html index.htm;
        }
        location /img/ {
            alias images/; #二选一  这个直接就是去 nginx配置的 /images文件夹下找
            # root images; #遇到访问/img时,去 nginx配置的 /images/img文件夹下找
        }
        location /api {
            #代理到自己node服务上去了
            proxy_pass http://127.0.0.1:8010;
        }
        # location ~ /\.png {
        #    deny  all;
        # }
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            try_files $uri $uri/ /index.html;
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

友情链接更多精彩内容