Ngnix配置-路由转发

不暴露端口版的访问步骤

浏览器输入网址 http://limshz.test-web.com/ 或者 https://limshz.test-web.com/

--> 系统查 hosts 文件

--> 拿到 IP(127.0.0.1)

--> 建立连接到 nginx

--> nginx 通过 server_name 处理请求

http配置步骤

  1. 修改host文件(Windows在: C:\Windows\System32\drivers\etc),配置如下

    127.0.0.1  limshz.test-web.com
    
  2. 修改 nginx文件 , 在config里的 ngnix.conf

events {
    worker_connections 1024;
}

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

    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 500m;

    upstream limshz {
        server 10.201.81.169:8001;
        server 10.201.81.169:8002;
        server 10.201.81.169:8003;
    }

    server {
        listen 80;
        server_name limshz.test-web.com

        location / {
            proxy_pass http://limshz;
        }
    }
}

  1. CMD命令行重启 nginx

    nginx.exe -s reload
    
  2. 访问地址: http://limshz.test-web.com/ 测试,浏览器能打开则配置成功

https配置步骤

  1. 修改host文件(Windows在: C:\Windows\System32\drivers\etc),配置如下

    127.0.0.1  limshz.test-web.com
    
  2. 修改 nginx文件 , 在config里的 ngnix.conf

    events {
        worker_connections 1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
        client_max_body_size 500m;
    
        upstream limshz {
            server 10.201.81.169:8001;
            server 10.201.81.169:8002;
            server 10.201.81.169:8003;
        }
    
        server {
            listen 443 ssl;
            server_name limshz.test-web.com;
    
            ssl_certificate     E:/Env/nginx-1.24.0/ssl/ssl-evb.crt;   # 证书路径
            ssl_certificate_key E:/Env/nginx-1.24.0/ssl/ssl-evb.key;   # 私钥路径
    
            ssl_protocols       TLSv1.2 TLSv1.3;
            ssl_ciphers         HIGH:!aNULL:!MD5;
    
            location / {
                proxy_pass http://limshz;
            }
        }
    
        # 自动把 HTTP 重定向到 HTTPS
        server {
            listen 80;
            server_name limshz.test-web.com;
            return 301 https://$host$request_uri;
        }
    }
    
    
    

    注意:ssl_certificate、ssl_certificate_key 要改成自己的路径!!!

  3. CMD命令行重启 nginx

    nginx.exe -s reload
    
  4. 访问地址: https://limshz.test-web.com/ 测试,浏览器能打开则配置成功

posted @ 2025-06-11 17:09  QLuffe  阅读(32)  评论(0)    收藏  举报