-

nginx

Nginx是一个高性能的HTTP和反向代理服务器,占用资源少,并发能力强。

nginx下载
下载地址: https://nginx.org/en/download.html

nginx安装
# 启动Nginx: 进入nginx目录 cmd >> start nginx
# 退出Nginx: nginx -s quit
# 测试Nginx配置: 进入nginx目录 cmd >> nginx -t
# 重启Nginx配置: 进入nginx目录 cmd >> nginx -s reload
# 结束所有nginx进程 cmd >> taskkill /F /IM nginx.exe
注意:nginx -t 在测试nginx是否配置成功的时候,有时提示不成功,那是nginx.conf配置文件有BOM,删除BOM就可以
nginx反向代理
nginx反向代理配置
修改 nginx\conf\nginx.conf 文件里的 proxy_pass 对应到网址
#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    # 服务器配置
    server {
        listen 80;
        server_name localhost;
        
        # 反向代理配置
        location / {
            proxy_pass http://xiyueta.com;
            
            # 设置代理请求头
            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_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
            
            # 缓冲区设置
            proxy_buffer_size 16k;
            proxy_buffers 4 64k;
        }
        
        # 错误页面配置
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}
        
检测并检测当前目录下的nginx.conf文件是否有BOM并删除BOM
cd > C:\nginx\conf > cmd
import os

def remove_bom_from_file(file_path):
    # 读取文件内容
    with open(file_path, 'rb') as file:
        content = file.read()
    
    # 检查是否有BOM
    if content.startswith(b'\xef\xbb\xbf'):
        print(f"检测到BOM在文件 {file_path}")
        
        # 删除BOM
        content = content[3:]
        
        # 将修改后的内容写回文件
        with open(file_path, 'wb') as file:
            file.write(content)
        
        print(f"已从文件 {file_path} 中删除BOM")
    else:
        print(f"文件 {file_path} 中没有检测到BOM")

# 获取当前目录
current_dir = os.getcwd()

# 构建nginx.conf的完整路径
nginx_conf_path = os.path.join(current_dir, 'nginx.conf')

# 检查文件是否存在
if os.path.exists(nginx_conf_path):
    remove_bom_from_file(nginx_conf_path)
else:
    print(f"在当前目录 {current_dir} 中未找到 nginx.conf 文件")
        
remove_bom.py 修改指定文件BOM
cd > C:\nginx\conf > cmd

import os
import codecs
import sys

def remove_bom(file_path):
    """
    移除文件的 BOM 标记
    """
    try:
        # 如果只提供文件名,使用当前目录
        if not os.path.dirname(file_path):
            file_path = os.path.join(os.getcwd(), file_path)
        
        # 检查文件是否存在
        if not os.path.exists(file_path):
            print(f"错误: 文件 '{file_path}' 不存在")
            return False

        print(f"正在处理文件: {file_path}")

        # 读取文件内容
        with open(file_path, 'rb') as f:
            content = f.read()

        # 检查是否有 BOM
        if content.startswith(codecs.BOM_UTF8):
            print("检测到 BOM 标记,正在移除...")
            
            # 移除 BOM 并写回文件
            with open(file_path, 'wb') as f:
                f.write(content[len(codecs.BOM_UTF8):])
            print("BOM 标记已成功移除")
            return True
        else:
            print("文件没有 BOM 标记")
            return True

    except Exception as e:
        print(f"处理文件时出错: {str(e)}")
        return False

def main():
    # 如果命令行提供了文件路径,使用它
    if len(sys.argv) > 1:
        file_path = sys.argv[1]
    else:
        # 否则请求用户输入
        file_path = input("请输入文件名或完整路径: ").strip('"')  # 移除可能的引号

    # 处理文件
    if remove_bom(file_path):
        print("处理完成")
    else:
        print("处理失败")

if __name__ == "__main__":
    main()