fp = '/opt/wecom-it-desk/nginx/nginx.conf' with open(fp) as f: c = f.read() patch = ''' # ------------------------------------------------------------------ # 真实 IP 还原(2026-06-15 v0.5.1 修复) # ------------------------------------------------------------------ set_real_ip_from 10.0.0.0/8; set_real_ip_from 172.16.0.0/12; set_real_ip_from 192.168.0.0/16; set_real_ip_from 10.212.0.0/16; real_ip_header X-Forwarded-For; real_ip_recursive on; ''' old = 'error_log /var/log/nginx/error.log warn;' new = old + patch new_c = c.replace(old, new, 1) with open(fp, 'w') as f: f.write(new_c) print('patched, +{} bytes'.format(len(new_c) - len(c)))