2

我有 nginx (1.15) 在 docker 容器内以 vagrant 的集群模式运行。这是我的 nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }

    #gzip  on;
    proxy_connect_timeout       1200s;
    proxy_send_timeout          3600s;
    proxy_read_timeout          3600s;
    include /etc/nginx/conf.d/*.conf;
}

这是我生成的配置文件 /etc/nginx/conf.d/app.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    client_max_body_size 100M;
    proxy_buffering on;
    proxy_intercept_errors off;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    location /jarvis/ {
        resolver 127.0.0.11 valid=1s;
        rewrite ^/jarvis(/.*) $1 break;
        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-Host $server_name;
        proxy_pass http://jarvis:8000;
        proxy_pass_request_headers on;
        proxy_redirect off;
        }

    location /nginx/ {
        resolver 127.0.0.11 valid=1s;
        rewrite ^/nginx(/.*) $1 break;
        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-Host $server_name;
        proxy_pass http://nginx:80;
        proxy_pass_request_headers on;
        proxy_redirect off;
        }
    }

当我在容器内curl http://localhost:8002/jarvis/health/时,它工作正常。

但是当我从浏览器访问时,nginx 在日志中抛出以下错误,在浏览器中抛出 404。

2018/10/01 05:11:07 [error] 61#61: *9 open() "/usr/share/nginx/html/jarvis/health" failed (2: No such file or directory), client: 10.255.0.2, server: localhost, request: "GET /jarvis/health HTTP/1.1", host: "localhost:8002"
10.255.0.2 - - [01/Oct/2018:05:11:07 +0000] "GET /jarvis/health HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
2018/10/01 05:11:07 [error] 61#61: *10 open() "/usr/share/nginx/html/robots.txt" failed (2: No such file or directory), client: 10.255.0.2, server: localhost, request: "GET /robots.txt HTTP/1.1", host: "localhost:8002"
10.255.0.2 - - [01/Oct/2018:05:11:07 +0000] "GET /robots.txt HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"

当我将两个配置文件合二为一时,它在 curl 和浏览器中都可以正常工作。此外,当我在 app.conf 中使用上游时(这不符合我的要求,因为 IP 是动态的),它适用于 curl 和浏览器。

你能告诉我我做错了什么吗,过去 3 天我一直遇到问题,找不到任何解决方案

4

0 回答 0