标签: nginx-location

if条件如何在nginx conf中的位置块内工作?

我已阅读 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

我想检查我的 rails 应用程序是否已经添加了标题 ( Access-Control-Allow-Origin),如果还没有添加标题。

这里的例子试图解释ifnginx.conf中条件的行为http://agentzh.blogspot.in/2011/03/how-nginx-location-if-works.html

但我没有理解它。我的问题之一是当他们说指令或阶段指令时是什么意思?

我自己尝试了一些东西。喜欢:

location / {
  add_header My-outer-header '*';
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true; 

  if($sent_http_access_control_allow_origin != /*){
    add_header My-inner-header '**';
  }
}
Run Code Online (Sandbox Code Playgroud)

这里当满足 if 条件时,只My-inner-header设置而不是My-outer-header

但是当我这样做时:

location / {
  add_header My-outer-header '*';
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true; 
  set $a 1;
  if($a == 1){
    add_header My-inner-header '**';
  }
}
Run Code Online (Sandbox Code Playgroud)

这里 $a 变量被设置为 …

nginx nginx-location

6
推荐指数
1
解决办法
2万
查看次数

Nginx 无法在 Windows 10 上创建目录并出现错误:nginx: CreateFile()...failed

我需要找到一种方法/设置来允许 Nginx 运行一个 .bat 文件,该文件将通过 WINDOWS 服务创建目录和文件;我的操作系统是Windows 10。目前,我们的 Windows 服务无法使用以下内容创建目录和文件:

CreateFile() "C:\someForlderName\build\distribution.\nginx/logs/error.log" failed (3: 系统找不到指定的路径)

不知何故,Nginx 没有足够的权限来执行写访问操作,例如在 /Nginx/ 目录中创建 /logs/ 和 /temp/ 等目录。

以前有人遇到过这个问题吗?

windows-services nginx windows-10 nginx-location

6
推荐指数
1
解决办法
1万
查看次数

Nginx动态定位路径配置

首先我想说,我对 nginx 还很陌生,我基本上只知道上周学到的东西。

也就是说,我目前有一个 Nginx 服务器,其标准配置为:

server {
  listen 80;
  server_name site.com;

  root /var/www/site.com/;
  index index.php;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location /microsite/first {
    try_files $uri $uri/ /microsite/first/;
  }

  location /microsite/second {
    try_files $uri $uri/ /microsite/second/;
  }

  ...
}
Run Code Online (Sandbox Code Playgroud)

这工作得很好,尽管对于我添加到现有微型站点的每个微型站点,它都需要添加一个引用新微型站点的路径的新位置。

我的问题是:是否可以动态设置location参数,使其捕获并引用microsite/目录中存在的任何子目录?

例如,沿着重写规则rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;(取自 nginx 站点)但应用于location参数的某些内容,例如:

location ~ ^/microsite/(.*)$ {
  try_files $uri $uri/ /microsite/$1/;
}
Run Code Online (Sandbox Code Playgroud)

在其中$1会捕获传入的子目录名称(.*)(我尝试了参考(另一个)Nginx 动态位置配置 …

nginx nginx-location

6
推荐指数
1
解决办法
2万
查看次数

Nginx 不区分大小写的 proxy_pass

我有一个名为 的站点http://example.com,其中运行着一个可以在http://example.com/app1. app1 位于 nginx 反向代理后面,如下所示:

location /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

将尾部斜杠添加到该proxy_pass字段可以让我“删除” URL 的 /app1/ 部分,至少就应用程序而言。所以 app1 认为它正在获取对根 url 的请求(例如,我在 app1 中有一个路由位于'/',而不是'/app1')。

但是,我想让 nginx 不区分大小写。所以无论我去http://example.com/App1还是http://example.com/APP1,它仍然应该将请求转发到app1,删除url的/app1/部分。

当我尝试使用 nginx 不区分大小写的规则时,它不允许将 URI 的其余部分转发到 app1。

location ~* /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade; …
Run Code Online (Sandbox Code Playgroud)

nginx nginx-location nginx-reverse-proxy

6
推荐指数
1
解决办法
5312
查看次数

proxy_pass 基于标头而不使用 if 条件

我正在寻找一种方法,将所有在 HTTP 标头中设置了Account-ID最后两位数字(十位数字)的account-id 的请求重新00路由到05(仅触及总流量的 5%)。此外,如果请求设置了 HTTP header Server-A,则无论设置的 account-id 是什么,该请求都应该转发到该服务器。否则,默认情况下,所有流量都应由 server-b 处理。我当前的位置规则如下所示:

location /rest/accounts/ {
  if ( $http_account_id ~ '([\d]{8})(0[0-4])') {
    proxy_pass http://server-a.company.com;
  }

  if ( $http_server_a = true) {
    proxy_pass http://server-a.company.com;
  }

  proxy_pass http://server-b.company.com;
}
Run Code Online (Sandbox Code Playgroud)

正如我在官方文档中看到的,这里if被认为是邪恶的

对于我的方法是否有更好的解决方案,而不被认为是邪恶的

nginx nginx-location

6
推荐指数
1
解决办法
2464
查看次数

Nginx try_files 不适用于默认的index.html

我正在使用 nginx 的 $geoip_country_code 模块根据 IP 重定向用户。我的配置代码如下所示-

server {
      listen 80;
      gzip on;
      server_name example.com;
      root html/example;
      location / {
        index index.html;
        try_files /$geoip_country_code/index.html /index.html;
      }
      location ~* \.(gif|jpg|jpeg|png|js|css)$ { }
  }
Run Code Online (Sandbox Code Playgroud)

想法是根据我已本地化的国家/地区重定向用户,否则用户将转到默认索引,即其他人通用的索引。当我在我的国家/地区的浏览器中打开它时,它可以完美地工作,因为我已经对其进行了本地化。但当从不同的国家/地区打开时,它会显示内部服务器错误。它不会进入默认索引页。

有人可以指出我做错了什么吗?

nginx devops nginx-location nginx-reverse-proxy

6
推荐指数
1
解决办法
4402
查看次数

nginx ssl_trusted_certificate 指令不起作用

当我尝试使用此配置启动 nginx 服务器时,出现错误

nginx: [emerg] no ssl_client_certificate for ssl_client_verify
Run Code Online (Sandbox Code Playgroud)

我的配置看起来像

# HTTPS server
server {
    listen       4443;
    server_name  localhost;
    ssl                  on;
    ssl_certificate      /home/user/conf/ssl/server.pem;
    ssl_certificate_key  /home/user/conf/ssl/server.pem;
    ssl_protocols        TLSv1.2;

    ssl_verify_client optional;
    ssl_trusted_certificate /home/user/ssl/certs/certificate_bundle.pem;

    include conf.d/api_proxy.conf;
}
Run Code Online (Sandbox Code Playgroud)

根据错误,我应该使用ssl_client_certificate指令,但根据文档,如果我不想将证书列表发送给客户端,我应该使用ssl_trusted_certificate.

http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_client_certificate

有人可以帮我弄清楚我错过了什么吗?

nginx nginx-location nginx-reverse-proxy nginx-status

6
推荐指数
1
解决办法
7662
查看次数

正则表达式查找匹配文件扩展名的文件,除非文件名包含字符串

我为 nginx 中的特定文件启用了缓存,如下所示:

location ~* \.(?:css|js)$ {
access_log off;
add_header Cache-Control "no-transform,public,max-age=31536000,s-max-age=31536000";
expires 1y;
}   
Run Code Online (Sandbox Code Playgroud)

我想在这里做的是排除所有与模式 i18n-*.js 匹配的文件,因此,缓存除以 i18n 开头的文件之外的所有 .js 文件。

我尝试进行否定查找以排除该模式,但由于非捕获组,它无法正常工作:

location ~* \.(?!i18n-.*\.js)(?:css|js)$ {
        access_log off;
        add_header Cache-Control "no-transform,public,max-age=31536000,s-max-age=31536000";
        expires 1y;
}
Run Code Online (Sandbox Code Playgroud)

这里的智能解决方案是什么?我不是正则表达式专家,所以简短的解释也会有帮助。

regex nginx browser-cache regex-lookarounds nginx-location

5
推荐指数
1
解决办法
4477
查看次数

POST 响应缓存在 nginx 中不起作用

我的任务是使用nginx实现微缓存策略,即缓存一些POST端点的响应几秒钟。

http部分nginx.conf我有以下内容:

proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;
Run Code Online (Sandbox Code Playgroud)

然后我locationserver

    location /my-url/ {
      root dir;
      client_max_body_size 50k;
      proxy_cache cache;
      proxy_cache_valid 10s;
      proxy_cache_methods POST;
      proxy_cache_key "$request_uri|$request_body";
      proxy_ignore_headers Vary;

      add_header X-Cached $upstream_cache_status;

      proxy_pass http://my-upstream;
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序位于输出处,如果我理解正确的my-upstreamCache-Control: max-age=10,应该使响应可缓存。

但是当我在短时间内(不到10秒)使用curl发出重复请求时

curl -v --data "a=b&c=d" https://my-host/my-url/1573
Run Code Online (Sandbox Code Playgroud)

它们全部到达后端(根据后端日志)。还有,X-Cached总是MISS

请求和响应如下:

> POST /my-url/1573 HTTP/1.1
> Host: my-host
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 113
> Content-Type: application/x-www-form-urlencoded
> 
* upload …
Run Code Online (Sandbox Code Playgroud)

caching nginx http-post nginx-location nginx-cache

5
推荐指数
1
解决办法
2561
查看次数

使用 nginx 在同一域上提供 React 前端和 php 后端

我有一个 React 前端和一个 Symfony 后端,我试图在同一个域上提供服务。React 前端需要提供资产(如果存在),否则回退到服务index.html

我想/api在请求 uri 中提供 php Symfony 应用程序。与 React 应用程序类似,我需要将所有请求转到该index.php文件。

前端服务正常,但 api 服务不正确。/api当我在浏览器中点击时,我从 nginx 收到 404 错误。

我觉得我已经很接近了,但由于某种原因 nginx 没有正确的$document_root. 我添加 header( X-script) 来测试变量是什么,我得到以下结果:

X-script: /usr/share/nginx/html/index.php
Run Code Online (Sandbox Code Playgroud)

这是我的 nginx 配置。

server {
    listen 80 default_server;
    index index.html index.htm;

    access_log /var/log/nginx/my-site.com.log;
    error_log /var/log/nginx/my-site.com-error.log error;

    charset utf-8;

    location /api {
        root /var/www/my-site.com/backend;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location / {
        root /var/www/my-site.com/frontend;
        try_files $uri /index.html;
    }
    location ~* …
Run Code Online (Sandbox Code Playgroud)

nginx symfony nginx-location

5
推荐指数
1
解决办法
4148
查看次数