nginx代理配置不转发请求到后端服务器 [英] nginx proxy config not forwarding requests to backend server

查看:2547
本文介绍了nginx代理配置不转发请求到后端服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的nginx.conf文件的相关部分. 当我删除conf文件中的初始位置块时,仅看到js | css ...请求转发到我的后端服务器.我要完成的工作是关闭这些扩展名文件的Nginx访问日志记录.

Below is the relevant section of my nginx.conf file. I only see the js|css... requests forward to my backend server when i remove the initial location block in the conf file. What im trying to accomplish is to turn off nginx access logging for files of those extensions.

有人知道一种有效的nginx配置技术,可让我关闭访问日志,但仍将这些请求转发到代理位置吗?

Anybody know a working nginx config technique to allow me to turn off the access logs yet still forward these requests to the proxy location?

...   
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            access_log   off;
}

location / {

if ($ignore_ua) {
    access_log off;
    return 200;
}
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7777/;
}

推荐答案

nginx选择一个位置块来处理请求.对于.js文件,将使用location ~* \.(js|css|png|jpg|jpeg|gif|ico)$块. location /块中的任何指令均不涉及.有关详细信息,请参见本文档.

nginx chooses a location block to process a request. In the case of .js files, your location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ block is used. None of the directives within the location / block are involved. See this document for details.

如果需要条件日志记录,则可以对access_log指令使用if=参数,而不要使用单独的位置块.有关示例,请参见本文档.

If you need conditional logging, you could use the if= parameter to the access_log directive instead of a separate location block. See this document for an example.

在您的情况下,可能看起来像这样:

In your case, it might look like this:

map $request_uri $loggable {
    default 1;
    \.(js|css|png|jpg|jpeg|gif|ico)(\?|$) 0;
}
access_log /path/to/access.log combined if=$loggable;

请注意,map指令位于http块中.

Note that the map directive goes in the http block.

这篇关于nginx代理配置不转发请求到后端服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆