-

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
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 文件")