标签 nginx 下的文章

要在 Nginx 配置中添加规则,使得所有 .map 后缀的请求返回 404,可以通过以下步骤进行配置:

  1. 打开你的 Nginx 配置文件(通常是 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/default)。
  2. 在 server 块中添加一个新的 location 块来匹配 .map 文件,并返回 404。

配置示例如下:

server {
    listen 80;
    server_name yourdomain.com;

    # 其他配置...

    # 添加 .map 后缀请求返回 404
    location ~* \.map$ {
        return 404;
    }

    # 其他配置...
}
  • location ~ .map$:此行匹配所有以 .map 结尾的请求,~ 表示不区分大小写。
  • return 404;:返回 HTTP 404 状态码。
  1. 保存配置文件后,重新加载 Nginx 配置使其生效:
sudo nginx -s reload

这样,所有以 .map 结尾的请求都会返回 404 错误。

今天迁移了一个网站到新服务器,用 certbot 安装证书后访问报错 ERR_SSL_PROTOCOL_ERROR

看了下 nginx 的错误日志:

2024/04/16 19:17:14 [crit] 1286606#1286606: *8 SSL_do_handshake() failed (SSL: error:0A0000BA:SSL routines::bad cipher) while SSL handshaking, client: xxx.xxx.xxx.xxx, server: 0.0.0.0:443

奇怪的是,其他网站都是正常的。

检查配置,发现迁移网站的 nginx 配置跟其他网站的区别有一个:

ssl_prefer_server_ciphers off;

off 改为 on 后 service nginx reload,问题解决。

需要注意的是,由于 ssl 是 certbot 自动配置的,所以这个配置项在 /etc/letsencrypt/options-ssl-nginx.conf 这个文件里。

PS: 如果不在这个文件夹里,可以参见你的站点配置文件 include 了哪个配置文件。

- 阅读剩余部分 -

macOS 升级到 14.0 Sonoma 之后,发现 brew 安装的 nginx 无法启动了。

sudo brew services list 查看状态:

nginx error  256 root /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

手动启动之:

$ sudo brew services start nginx 
Warning: Taking root:admin ownership of some nginx paths:
  /opt/homebrew/Cellar/nginx/1.25.2/bin
  /opt/homebrew/Cellar/nginx/1.25.2/bin/nginx
  /opt/homebrew/opt/nginx
  /opt/homebrew/opt/nginx/bin
  /opt/homebrew/var/homebrew/linked/nginx
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: nginx must be run as non-root to start at user login!
Bootstrap failed: 5: Input/output error
Error: Failure while executing; `/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.nginx.plist` exited with 5.

- 阅读剩余部分 -

迁移项目到新电脑上,同样的 Nginx 配置,结果在新电脑上发现了一个问题:

所有的资源文件(js/image/css等)报 403 Forbidden 错误。

因为配置文件一模一样,所以开始以为是 Nginx 版本问题,搜了一下网上没发现这个问题。

于是打开 Nginx 的错误日志,发现:

- 阅读剩余部分 -

Nginx 出现这个报错的原因通常是 client_max_body_size 的配置问题。

该配置项默认值仅为 1M,所以在上传大于 1M 的文件时报错。

需要在对应的站点 server 里修改此项配置,如下:

server {
    # ... 其他配置
    client_max_body_size 10M;
    # ... 其他配置
}