标签: nginx

如何配置 nginx 位置以共享常用配置选项?

如何为一组位置配置共享配置块?

    location / {

            proxy_pass        http://127.0.0.1:9000/;
            proxy_redirect    off;
            proxy_set_header  Host             $http_host;
            proxy_set_header  X-Real-IP        $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

            proxy_cache cache-test;
            proxy_cache_valid 200 302 24h;
            proxy_cache_valid 404 60s;
            add_header X-Cache-Status $upstream_cache_status;

    }


    location /api/0.1/user{
            proxy_cache_key /user/$http_authorization;
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试访问 /api/0.1/user 然后我会得到 404 因为它没有将请求传递给 127.0.0.1:9000

nginx

51
推荐指数
2
解决办法
4万
查看次数

nginx 在位置设置变量

我正在尝试优化我的 nginx 配置,因此可以设置一个变量,并且所有位置路径都会自动更新。我有四行有问题:

server_name php.domain.com;
root /srv/web/vhosts/php/web;
error_log /srv/web/vhosts/php/logs/error.log;
access_log /srv/web/vhosts/php/logs/access.log;
Run Code Online (Sandbox Code Playgroud)

我想要实现的是设置一个变量(在本例中为“php”)并将其包含到 config.php 中。

set $variable "php";
server_name $variable.domain.com;
root /srv/web/vhosts/$variable/web;
error_log /srv/web/vhosts/$variable/logs/error.log;
access_log /srv/web/vhosts/$variable/logs/access.log;
Run Code Online (Sandbox Code Playgroud)

然而,nginx 忽略了这个配置中的变量。我做错了什么还是不可能在位置路径中使用变量?

nginx

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

nginx 代理传递重定向忽略端口

因此,当我指向 nginx conf 中的 node.js 应用程序时,我正在设置一个虚拟路径。相关部分如下所示:

location /app {
  rewrite /app/(.*) /$1 break;
  proxy_pass http://localhost:3000;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Run Code Online (Sandbox Code Playgroud)

效果很好,除了当我的 node.js 应用程序(一个快速应用程序)调用重定向时。

例如,开发箱在端口 8080 上运行 nginx,因此节点应用程序根目录的 url 如下所示:

http://localhost:8080/app

当我从节点调用重定向到“/app”时,实际的重定向会转到:

http://本地主机/应用程序

nginx

50
推荐指数
4
解决办法
7万
查看次数

此处不允许使用 nginx 服务器指令

我知道那里有骗子,但在我的情况下我似乎无法解决这个问题。

我正在关注一篇关于使用 apache 将 nginx 设置为反向代理的文章。

我得到这个错误:

nginx: [emerg] "server" directive is not allowed here in 
       /etc/nginx/v.hosts/mydomain.com.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)

我的/etc/nginx/nginx.conf看起来像这样:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream; 
    access_log  /var/log/nginx/access.log  main;  
    charset   utf-8;
    keepalive_timeout  65;
    server_tokens       off;
    tcp_nopush          on;
    tcp_nodelay         off;

    server {
          listen 80;
          server_name  _;          
          root   /usr/share/nginx/html;
          index  index.html index.htm;     
       }
    }
include  v.hosts/*.conf;
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/v.hosts/mydomain.com.conf看起来像这样:

server {
      listen 80;
      server_name  mydomain.com;

       access_log  off;
       error_log off;

      location / {
        proxy_pass http://127.0.0.1:81;
        proxy_set_header        X-Real-IP …
Run Code Online (Sandbox Code Playgroud)

configuration nginx

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

如何限制对目录和子目录的访问

我需要限制对目录“testdir”中任何文件或子目录的访问。我的配置:

...
location ~* ^.+\.(jpg|txt)$ {
            root   /var/www/site;
        }
location /testdir {
        deny all;
        return 404;
        }
...
Run Code Online (Sandbox Code Playgroud)

在我的配置中,我对 /testdir/jpg_or_txt-files 没有任何限制。怎么做?

rewrite nginx deny

49
推荐指数
4
解决办法
11万
查看次数

nginx:无权限绑定端口 8090 但它绑定到 80 和 8080

我正在努力解决一些与权限相关的奇怪行为:当我配置 nginx 以侦听端口 8080 时,一切都按预期工作,但是当我使用任何其他端口时,我会得到类似的结果

2014/01/10 09:20:02 [emerg] 30181#0: bind() to 0.0.0.0:8090 failed (13: Permission denied)
Run Code Online (Sandbox Code Playgroud)

/var/log/nginx/error.log

我不知道从哪里看,所以我真的不知道配置的哪些部分可能很有趣。

在 nginx.conf 中,nginx 被配置为作为 nginx 运行:

user  nginx;
Run Code Online (Sandbox Code Playgroud)

用户 nginx 也在另一个组“git”中

在站点配置中,我试着这样听:

server {
    listen 8090; #does not work
    #listen 8080; #works
    #listen 9090; #does not work
    #listen 9090 default; #does not work neighter
    #listen 80; #works!
    server_name <some IP>;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我只有一个监听端口 443。

当我启动一些其他服务时,例如SimpleHTTPServer端口 8090 等,作为非 root 一切正常:

$ python -m SimpleHTTPServer 8090
Serving HTTP on 0.0.0.0 port …
Run Code Online (Sandbox Code Playgroud)

permissions nginx fedora port

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

使用上游 SSL 将 Nginx 配置为反向代理

我尝试将 Nginx 服务器配置为反向代理,以便它从客户端收到的 https 请求也通过 https 转发到上游服务器。

这是我使用的配置:

http {

    # enable reverse proxy
    proxy_redirect              off;
    proxy_set_header            Host            $http_host;
    proxy_set_header            X-Real-IP       $remote_addr;
    proxy_set_header            X-Forwared-For  $proxy_add_x_forwarded_for;

    upstream streaming_example_com 
    {
          server WEBSERVER_IP:443; 
    }

    server 
    {
        listen      443 default ssl;
        server_name streaming.example.com;
        access_log  /tmp/nginx_reverse_access.log;
        error_log   /tmp/nginx_reverse_error.log;
        root        /usr/local/nginx/html;
        index       index.html;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_certificate /etc/nginx/ssl/example.com.crt;
        ssl_certificate_key /etc/nginx/ssl/example.com.key;
        ssl_verify_client off;
        ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers RC4:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;


        location /
        {
            proxy_pass  https://streaming_example_com;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

无论如何,当我尝试使用反向代理访问文件时,这是我在反向代理日志中得到的错误:

2014/03/20 12:09:07 [错误] 4113079#0:*1 SSL_do_handshake() …

ssl nginx reverse-proxy

49
推荐指数
3
解决办法
10万
查看次数

使用 NGINX 将 HTTPS 请求代理到 HTTP 后端

我将 nginx 配置为我的外部可见网络服务器,它通过 HTTP 与后端通信。

我想要实现的场景是:

  1. 客户端向 nginx 发出 HTTP 请求,该请求被重定向到相同的 URL 但通过 HTTPS
  2. nginx 代理通过 HTTP 向后端请求
  3. nginx 通过 HTTP 从后端接收响应。
  4. nginx 通过 HTTPS 将其传递回客户端

我当前的配置(后端配置正确)是:

服务器 {
        听80;
        server_name 本地主机;

        位置 ~ .* {
            proxy_pass http://backend;
            proxy_redirect http://backend https://$host;
            proxy_set_header 主机 $host;
            }
        }

我的问题是对客户端的响应(第 4 步)是通过 HTTP 而不是 HTTPS 发送的。有任何想法吗?

nginx https reverse-proxy

47
推荐指数
2
解决办法
13万
查看次数

为什么将 Nginx 设置为反向代理是个好主意?

我有一个在 Gunicorn 上运行的 Django 站点,通过 Nginx 使用反向代理。Nginx 不就是额外的不必要的开销吗?在 Gunicorn 之上添加它有什么帮助?

nginx web-server reverse-proxy django

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

Nginx worker_connections 的最佳值

Nginx worker_connections“设置了一个工作进程可以打开的最大并发连接数。这个数字包括所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接。另一个考虑因素是实际的同时连接数不能超过当前最大打开文件数限制”。我对此有几个疑问:

  1. 为此,最佳或推荐值应该是多少?
  2. 使用大量工作连接的缺点是什么?

nginx scalability connection worker-process

47
推荐指数
3
解决办法
7万
查看次数