解决 macOS 升级 Big Sur 后 /etc/hosts 文件不生效的问题

macOS 升级 Big Sur 后,使用浏览器时,可能出现 /etc/hosts 文件中某些条目不生效的问题,尤其是 .com 域名的条目(目前 .local 域名的条目不受影响)。

* 上有相关的讨论:https://*.com/questions/65972371/why-does-etc-hosts-not-work-anymore-for-some-websites-on-big-sur

这里提供一个 workaround 方案。

1. 运行 NGINX 作为正向 HTTPS 代理

  • 创建文件 nginx.conf
daemon off;
worker_processes auto;
worker_rlimit_nofile 65535;

events {
  multi_accept on;
  worker_connections 65535;
}

http {
  charset utf-8;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  server_tokens off;
  log_not_found off;
  types_hash_max_size 2048;
  types_hash_bucket_size 64;
  client_max_body_size 16M;

  # MIME
  include mime.types;
  default_type application/octet-stream;

  # Logging
  access_log /dev/stdout;
  error_log /dev/stdout warn;

  # DNS
  resolver 223.5.5.5 223.6.6.6;
  resolver_timeout 2s;

  # Proxy
  proxy_connect_timeout 10s;
  proxy_send_timeout 10s;
  proxy_read_timeout 10s;
  proxy_buffer_size 4k;
  proxy_buffers 4 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;

  server {
    listen 8888;
    server_name <TODO-DOMAIN>;

    # forward proxy for CONNECT request
    proxy_connect;
    proxy_connect_address <TODO-IP>;
    proxy_connect_connect_timeout 10s;
    proxy_connect_read_timeout 10s;
    proxy_connect_send_timeout 10s;

    # forward proxy for non-CONNECT request
    location / {
      return 500;
    }
  }

  server {
    listen 8888;
    server_name ~.+;

    location / {
      return 404;
    }
  }
}

注意:需要将 <TODO-DOMAIN><TODO-IP> 替换为原 /etc/hosts 文件中对应的域名和IP地址,可以(按需复制)包含多个 server 配置块。

  • 创建 docker-compose.yaml
version: "3"
services:
  nginx_proxy:
    image: reiz/nginx_proxy
    volumes:
    - ./nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
    ports:
    - 8888:8888
    restart: unless-stopped

注意:这里暴露服务到本地端口 8888

  • 后台运行 NGINX(正向 HTTPS 代理)
 docker-compose up -d

2. Chrome/Edge 浏览器配置 Proxy SwitchyOmega 插件

解决 macOS 升级 Big Sur 后 /etc/hosts 文件不生效的问题

上一篇:PS制作质感的银色网纹效果的文字教程


下一篇:Linux:CentOS7下安装Redis的详细步骤