试图转发一些路径到一个特定的容器与nginx但它一直带我到根路径

mspsb9vt  于 2023-03-29  发布在  Nginx
关注(0)|答案(1)|浏览(133)

bounty将在6天后过期。回答此问题可获得+50声望奖励。ec08b914希望引起更多人关注此问题。

我有三个容器:nginxwordpressspecial-container。我希望special-container处理我的域的两个特定路径,wordpress处理其他所有路径。下面是我的nginx.conf

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/html;
    }

    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;

    index index.php index.html index.htm;

    root /var/www/html;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
    add_header Strict-Transport-Security max-age=0;

    location = /special_path_1 {
      return 301 /special_path_1/;
    }

    location /special_path_1/ {
        proxy_redirect off;
        proxy_pass http://special-container:200;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    location = /special_path_2 {
      return 301 /special_path_2/;
    }

    location /special_path_2/ {
        proxy_redirect off;
        proxy_pass http://special-container:201;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
}

当我在容器nginx内运行curl http://special-container:200时,我可以看到来自special-container的正确响应。然而,当我使用Chrome访问https://example.com/special_path_1时,我只是不断被重定向到https://example.com。清除cookie和使用隐身没有帮助。但是当我访问一些随机路径,如https://example.com/asdfsa时,我看到WordPress返回了一个404页面。WordPress上的其他一切也都运行正常。
下面是nginx的日志:

XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:21 +0800] "GET /special_path_1 HTTP/2.0" 301 162 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:21 +0800] "GET /special_path_1/ HTTP/2.0" 307 37 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET / HTTP/2.0" 200 36865 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET /wp-includes/blocks/navigation/style.min.css?ver=6.1.1 HTTP/2.0" 200 15342 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET /wp-includes/js/wp-emoji-release.min.js?ver=6.1.1 HTTP/2.0" 200 18617 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET /wp-content/themes/livro/style.css?ver=1.0.19 HTTP/2.0" 200 6052 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET /wp-includes/blocks/navigation/view.min.js?ver=c24330f635f5cb9d5e0e HTTP/2.0" 200 1144 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:22 +0800] "GET /wp-includes/blocks/navigation/view-modal.min.js?ver=45f05135277abf0b0408 HTTP/2.0" 200 7779 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:23 +0800] "GET /wp-content/themes/livro/assets/fonts/Newsreader.woff2 HTTP/2.0" 200 214880 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:23 +0800] "GET /wp-content/themes/livro/assets/fonts/Newsreader-italic.woff2 HTTP/2.0" 200 239844 "https://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"


XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:38 +0800] "GET /asdfsa HTTP/2.0" 404 35922 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-includes/blocks/navigation/style.min.css?ver=6.1.1 HTTP/2.0" 200 15342 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-includes/js/wp-emoji-release.min.js?ver=6.1.1 HTTP/2.0" 200 18617 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-content/themes/livro/style.css?ver=1.0.19 HTTP/2.0" 200 6052 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-includes/blocks/navigation/view.min.js?ver=c24330f635f5cb9d5e0e HTTP/2.0" 200 1144 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-includes/blocks/navigation/view-modal.min.js?ver=45f05135277abf0b0408 HTTP/2.0" 200 7779 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:39 +0800] "GET /wp-content/themes/livro/assets/fonts/Newsreader.woff2 HTTP/2.0" 200 214880 "https://example.com/asdfsa" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"
XXX.XXX.XXX.XXX - - [26/Mar/2023:01:29:57 +0800] "POST /wp-admin/admin-ajax.php HTTP/2.0" 200 47 "https://example.com/wp-admin/theme-install.php?search=Quadrat" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-"

为什么我在这里得到一个307重定向?我做错了什么?提前感谢。
===编辑===
curl -I --location https://example.com/special_path_1的结果:

HTTP/2 301 
server: nginx
date: Sat, 25 Mar 2023 17:11:59 GMT
content-type: text/html
content-length: 162
location: https://example.com/special_path_1/
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src * data: 'unsafe-eval' 'unsafe-inline'
strict-transport-security: max-age=0

HTTP/2 404 
server: nginx
date: Sat, 25 Mar 2023 17:11:59 GMT
content-type: text/plain
content-length: 18
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src * data: 'unsafe-eval' 'unsafe-inline'
e4yzc0pl

e4yzc0pl1#

你需要在proxy_pass的末尾放一个/到special-container. proxy_pass http://special-container:200/。如果没有/,当你请求https://example.com/special_path_1时,这个请求发送到http://special-container:200/special_path_1,然后得到404,如果有/,这个请求发送到http://special-container:200/,然后得到200。
这一部分必须是这样的:

location /special_path_1/ {
        proxy_redirect off;
        proxy_pass http://special-container:200/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
}

相关问题