nginx的proxy_temp目录权限为nobody nginx -t操作

本文记述了一次使用nginx-t检查配置时遇到的诡异事件,因执行用户与服务启动用户不一致,导致proxy_temp目录权限被更改,进而引发前端请求失败。通过此案例,提醒我们在执行类似操作时需注意用户权限,避免不必要的服务中断。

nginx -t 之诡异事件
proxy_temp目录的所有者和所属组自动被改为nobody,导致前端请求失败

我之前在两台机器的nginx.conf中添加了配置,白天业务还在作业不能重启,我就先nginx -t 检查下修改完格式有没有报错

就在我操作完nginx -t后,前台页面发现访问异常,查看nginx的error.log,发现请求没有权限访问proxy_temp这个临时静态文件目录,所以才会报错
在这里插入图片描述

查看nginx下的proxy_temp目录发现所有者和所属组都被改成了nobody

在这里插入图片描述

后来和一个大牛聊天聊到这个事情,我们的nginx服务启动用户是nginx,当时我执行nginx -t 操作时用的是root用户,如果执行nginx -t的用户不是nginx目录的所有者,就会强行改变下面临时目录的权限

我一直觉得nginx -t是没有任何风险的操作(手动捂脸…),SO…还是认真学习吧

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # HTTPS server server { listen 80; server_name www.wyqg.top; # 关键:设置允许的最大请求体大小(1000M,可根据需要调整) client_max_body_size 1000m; # 关键规则:优先匹配/incorruptFront/wechat路径 # 放在最前面以确保最高优先级 location ^~ /incorruptFront/wechat { # 核心调试标识:只要匹配到该规则,就会返回这个响应头 add_header X-Rule-Matched "incorruptFront-wechat" always; proxy_pass http://218.31.203.46:8081; 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_set_header X-Forwarded-Proto $scheme; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'Content-Type, Authorization'; add_header Cache-Control "no-store, no-cache, must-revalidate"; } # /api路径转发规则 location /api { rewrite ^/api/(.*)$ /$1 break; proxy_pass http://218.31.203.46:8081; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'Content-Type'; } location = /file/upload { # 关键:关闭请求缓冲(立即转发到后端) proxy_request_buffering off; # 核心代理配置 proxy_pass http://218.31.203.46:8081; # 头部透传设置 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 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; # 临时文件路径(需要有写入权限proxy_temp_path C:\nginx-1.26.3\client_body; } # 静态资源缓存配置 location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg)$ { root dist_FWH/build/web; expires 1d; # 静态资源缓存1天 add_header Cache-Control "public, max-age=86400"; } # 前端静态资源(默认规则,放在最后) location / { root dist_FWH/build/web; index index.html; try_files $uri $uri/ /index.html; } } #后台管理项目 server { listen 8080 ; server_name www.wyqg.top; # 替换为您的域名或服务器IP root dist_HTGL; # 应该是目录,不是文件 # 关键:设置允许的最大请求体大小(1000M,可根据需要调整) client_max_body_size 1000m; # 启用 Gzip 静态文件支持 gzip_static on; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 路由 history 模式支持 location / { index index.html; try_files $uri $uri/ /index.html; # 缓存配置 if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) { expires 365d; add_header Cache-Control "public, immutable"; } } # 8080端口server块中必须包含这段配置 location /statics/ { proxy_pass http://218.31.203.46:9300; # 关键:指向图片服务器IP+端口 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # 静态资源目录 location /static/ { alias dist_HTGL/static/; expires max; add_header Cache-Control "public, immutable"; access_log off; } # API 代理配置 location /prod-api/ { proxy_pass http://218.31.203.46:8081/; # 请求头处理 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_set_header X-Forwarded-Proto $scheme; # WebSocket 支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # 超时设置 proxy_connect_timeout 60s; proxy_read_timeout 600s; proxy_send_timeout 600s; } # 禁止访问敏感文件 location ~ /\.(env|git) { deny all; return 403; } } } 报错:C:\nginx-1.26.3>nginx.exe -t nginx: the configuration file C:\nginx-1.26.3/conf/nginx.conf syntax is ok nginx: [emerg] CreateDirectory() "C: ginx-1.26.3\client_body" failed (123: The filename, directory name, or volume label syntax is incorrect) nginx: configuration file C:\nginx-1.26.3/conf/nginx.conf test failed
最新发布
09-02
<think> 我们之前已经分析过,问题在于Windows路径中的反斜杠在Nginx配置文件中被当作转义字符解析了。 解决方案是使用正斜杠(/)代替反斜杠(\),或者使用双反斜杠(\\)进行转义。 在配置文件中,将路径修改为: proxy_temp_path C:/nginx-1.26.3/client_body; 或者 proxy_temp_path C:\\nginx-1.26.3\\client_body; 另外,请确保该目录已经被创建,并且Nginx进程有写入权限。 但是,根据错误信息,我们看到错误依然存在。让我们再次检查错误信息: CreateDirectory() "C:\nginx-1.26.3\client_body" failed (123: The filename, directory name, or volume label syntax is incorrect) 注意,这里显示的是:C: ginx-1.26.3\client_body 这是因为在配置中使用了单反斜杠,导致'\n'被转义为换行符。 因此,我们需要修改配置文件中的路径写法。 我们采用正斜杠的写法,因为它在Windows和Linux中都适用,而且不需要转义。 修改步骤: 1. 打开配置文件,找到`proxy_temp_path`指令,将其改为: proxy_temp_path C:/nginx-1.26.3/client_body; 2. 手动创建该目录(如果不存在): 在C盘下创建目录:C:\nginx-1.26.3\client_body 3. 给该目录设置写入权限(如果Nginx运行账户没有权限): 右键目录 -> 属性 -> 安全 -> 编辑 -> 添加用户(如Everyone)并赋予完全控制权限。 4. 测试配置文件并重新加载。 如果问题仍然存在,可能是由于路径中包含了特殊字符(如空格)或权限问题,但这里路径没有空格。 另外,注意:在Windows中,Nginx的临时目录最好不要包含空格,并且路径尽量短。 还有一种可能是,Nginx在创建多级目录时失败,因为父目录不存在。因此,请确保C:\nginx-1.26.3目录已经存在。 如果上述方法不行,我们可以尝试使用相对路径,但相对路径是相对于Nginx的安装目录吗?在Windows中,通常我们使用绝对路径。 此外,我们还可以尝试使用8.3短路径格式,但一般情况下不需要。 另一种方案:我们可以将临时目录设置Nginx的安装目录下,比如:proxy_temp_path client_body_temp; 这样,临时目录将位于Nginx的安装目录下的client_body_temp文件夹。然后我们手动创建该文件夹并赋权。 如果问题仍然存在,我们可以暂时移除`proxy_temp_path`指令,使用默认的临时目录(通常在Nginx安装目录下的client_body_temp),然后确保该目录存在且有写入权限。 但是,考虑到我们之前已经指定了一个绝对路径,并且出现了转义问题,现在我们已经改用正斜杠,应该可以解决。 我们再次修改配置,并测试: 修改后的配置片段: ```nginx location = /file/upload { # ... 其他配置 ... proxy_temp_path C:/nginx-1.26.3/client_body; } ``` 然后执行: nginx -t 如果还报错,请检查错误信息。 这里我们注意到,错误信息中显示的是: CreateDirectory() "C:\nginx-1.26.3\client_body" failed 但是我们在配置中写的是`C:/nginx-1.26.3/client_body`,所以错误信息中显示的依然是反斜杠?这是因为在Windows中,系统API会使用反斜杠。但Nginx在解析配置时,会将正斜杠转换为系统路径分隔符。如果配置中使用了正斜杠,Nginx会正确处理。 因此,我们怀疑可能是配置修改没有保存?请确保修改后保存了配置文件。 如果保存后仍然报错,我们可以尝试将路径用双引号括起来,以避免任何可能的解析问题: proxy_temp_path "C:/nginx-1.26.3/client_body"; 或者: proxy_temp_path "C:\\nginx-1.26.3\\client_body"; 但根据Nginx官方文档,路径字符串不需要引号。不过,我们可以尝试一下。 如果以上都不行,可能是权限问题,请确保Nginx进程有权限在C盘创建目录。在Windows中,Nginx默认以当前用户或系统服务账户运行,可能没有在C盘根目录创建目录权限?我们可以尝试将临时目录放在Nginx安装目录下,比如: proxy_temp_path client_body_temp; 然后在Nginx安装目录下创建client_body_temp目录,并赋予Everyone完全控制权限。 下面我们给出一个修改后的配置,放在Nginx安装目录下: ```nginx location = /file/upload { # ... 其他配置 ... proxy_temp_path client_body_temp; } ``` 然后创建目录:C:\nginx-1.26.3\client_body_temp,并设置权限。 然后测试配置。 由于我们已经在Nginx的安装目录下,所以相对路径就是安装目录。 我们更倾向于使用绝对路径,但为了避免问题,也可以使用相对路径(相对于安装目录)。不过,我们也可以使用Nginx内置变量`$nginx_home`?实际上,Nginx没有这样的内置变量。所以我们使用相对路径时,它相对于Nginx的安装目录(即nginx.exe所在目录)。 因此,修改为相对路径: proxy_temp_path client_body_temp; 然后确保在Nginx安装目录(C:\nginx-1.26.3)下存在client_body_temp文件夹。 现在,我们按照这个思路修改配置。 但是,我们首先尝试用双引号包裹绝对路径: proxy_temp_path "C:/nginx-1.26.3/client_body"; 如果不行,再尝试相对路径。 请按照以下步骤操作: 1. 修改配置为: proxy_temp_path "C:/nginx-1.26.3/client_body"; 2. 保存,运行nginx -t 如果还报错,则: 1. 修改配置为: proxy_temp_path client_body_temp; 2. 在C:\nginx-1.26.3目录下创建client_body_temp文件夹。 3. 给该文件夹设置写入权限。 4. 测试配置。 注意:在Windows中,路径中不要包含空格,并且尽量使用英文命名。 我们也可以考虑使用Windows的环境变量,比如`${TEMP}`,但Nginx不支持这样的变量。所以还是使用绝对路径或者相对路径。 最后,如果上述方法均无效,我们可以考虑注释掉`proxy_temp_path`指令,使用默认的临时目录(在Nginx安装目录下的client_body_temp)。默认情况下,Nginx会自动创建该目录吗?在Windows中,Nginx不会自动创建,所以我们需要手动创建并赋权。 因此,我们也可以删除`proxy_temp_path`指令,然后手动创建Nginx安装目录下的client_body_temp目录,并赋权。 总结:优先尝试使用绝对路径(正斜杠)并确保目录存在且有权限,如果不行则使用相对路径,或者使用默认设置(不指定proxy_temp_path,但确保默认目录存在)。 下面我们给出一个具体的配置修改方案,并附上操作步骤。 </think> ### 错误分析与解决方案 您遇到的错误是因为 Windows 路径中的反斜杠 `\` 在 Nginx 配置中被视为转义字符。当 Nginx 解析 `C:\nginx-1.26.3\client_body` 时: - `\n` 被解释为换行符 - `\c` 被解释为特殊字符 导致路径被错误解析为 `C:\nginx-1.26.3\client_body` → `C:\nginx-1.26.3lient_body` #### 修复方案(任选其一): **方案1:使用正斜杠(推荐,跨平台兼容)** ```nginx location = /file/upload { # ... [其他配置保持不变] ... proxy_temp_path C:/nginx-1.26.3/client_body; } ``` **方案2:使用双反斜杠转义** ```nginx location = /file/upload { # ... [其他配置保持不变] ... proxy_temp_path C:\\nginx-1.26.3\\client_body; } ``` **方案3:使用相对路径(最简单)** ```nginx location = /file/upload { # ... [其他配置保持不变] ... proxy_temp_path client_body_temp; # 使用相对路径 } ``` ### 完整操作步骤: 1. **创建临时目录**: ```cmd mkdir C:\nginx-1.26.3\client_body ``` 2. **修改 Nginx 配置**: ```nginx location = /file/upload { proxy_request_buffering off; proxy_pass http://218.31.203.46:8081; 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 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_temp_path C:/nginx-1.26.3/client_body; # 使用正斜杠 } ``` 3. **赋权(重要)**: ```cmd icacls "C:\nginx-1.26.3\client_body" /grant "Everyone:(OI)(CI)F" ``` 4. **测试并重载配置**: ```cmd nginx -t nginx -s reload ``` ### 备选方案:使用内存缓冲(提升性能) 如果文件上传性能仍不理想,可添加内存缓冲: ```nginx location = /file/upload { # ... [其他配置] ... # 启用内存缓冲(100MB以内文件使用内存) client_body_buffer_size 100m; # 设置临时目录 proxy_temp_path C:/nginx-1.26.3/client_body; } ``` ### 验证方法: 1. 测试配置文件: ```cmd nginx -t ``` 应返回:`configuration file ... test is successful` 2. 上传测试文件: ```cmd curl -X POST -F "file=@test.zip" http://localhost/file/upload ``` 3. 检查临时目录文件: ```cmd dir C:\nginx-1.26.3\client_body ``` 应能看到 `.tmp` 后缀的临时文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值