nginx容器80端口重定向到未配置的url

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

我在我的linux系统中创建了一个nginx容器来做一些测试。经过一些测试,我想在“location /quince-builds”中注解代码,并希望我可以得到其他配置。然而,每个uri都重定向到一个带有“/quince-builds”的url。例如,当我请求http://192.168.39.7/myrtle-builds时,显示http://192.168.39.7/myrtle-builds/quince-builds未找到。当我请求http://192.168.39.7时,显示http://192.168.39.7/quince-builds
我关闭了防火墙

sudo systemctl stop firewalld

这是我的docker compose文件:这个nginx文件夹是另一个nginx容器的init Boot 的副本。

version: '3.6'
services:
  nginx:
    image: nginx
    ports:
      - 80:80
      - 90:90
    container_name: my-nginx
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ${PWD}/nginx:/etc/nginx

这是我的nginx配置文件:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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"';

    log_format  more  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"$proxy_host" $proxy_add_x_forwarded_for - "$scheme"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    upstream test {
        # zone upstream_dynamic 64k;
        server 192.168.111.64:8080;
        # server backend1.example.com      weight=5;
        # server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
        # server 192.0.2.1                 max_fails=3;
        # server backend3.example.com      resolve;
        # server backend4.example.com      service=http resolve;

        # server backup1.example.com:8080  backup;
        # server backup2.example.com:8080  backup;
    }

    server{
        listen 80;
        server_name  192.168.39.7;
        location / {
            #proxy_set_header Host $remote_addr;
            #proxy_pass http://192.168.111.64/quince-builds;
            access_log  /var/log/nginx/access-test.log  more;
            #return 702;
        }
        location /myrtle-builds{
            proxy_pass http://192.168.111.63/myrtle-builds;
        }
        location /quince-builds{
            proxy_pass http://192.168.111.64/quince-builds;
        }
        location /qb{
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            #proxy_pass http://192.168.111.64/quince-builds;
            return 701;
        }
        location /push{
            #proxy_set_header X-Real-IP $remote_addr;
            #proxy_set_header Host $proxy_host;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://192.168.111.64:8080/generic-webhook-trigger/invoke?token=quince-cloud-push;
        }
        location = /pushtest{
            client_body_buffer_size     10M;
            #client_max_body_size        10M;
            proxy_pass http://192.168.32.177:9898/report/update_data;
        }
         location ~* /icons{
            # When request /icons*******, proxy to /usr/share/icons*****
            root /usr/share;
            #proxy_pass http://192.168.39.7;
            access_log  /var/log/nginx/access-test.log  more;
        }
    }


}

不知道这是否有帮助:

root@7f21cf089c06:/# nginx -q
2023/03/21 08:03:04 [emerg] 53#53: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/03/21 08:03:04 [emerg] 53#53: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2023/03/21 08:03:04 [notice] 53#53: try again to bind() after 500ms
2023/03/21 08:03:04 [emerg] 53#53: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/03/21 08:03:04 [emerg] 53#53: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2023/03/21 08:03:04 [notice] 53#53: try again to bind() after 500ms
2023/03/21 08:03:04 [emerg] 53#53: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/03/21 08:03:04 [emerg] 53#53: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2023/03/21 08:03:04 [notice] 53#53: try again to bind() after 500ms
2023/03/21 08:03:04 [emerg] 53#53: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/03/21 08:03:04 [emerg] 53#53: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2023/03/21 08:03:04 [notice] 53#53: try again to bind() after 500ms
2023/03/21 08:03:04 [emerg] 53#53: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/03/21 08:03:04 [emerg] 53#53: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2023/03/21 08:03:04 [notice] 53#53: try again to bind() after 500ms
2023/03/21 08:03:04 [emerg] 53#53: still could not bind()
nginx: [emerg] still could not bind()

我试着rm容器并运行一个简单的nginx容器:

docker run -dit -p 80:80 nginx

当我请求http://192.168.39.7时,它仍然重定向到http://192.168.39.7/quince-builds
当我更改端口时,它可以工作,我可以通过执行以下操作看到“欢迎使用nginx”页面:

docker run -dit -p 81:80 nginx
request http://192.168.39.7:81

这是我删除nginx容器后使用的端口:

[tcnsh@localhost nginx-test]$ sudo netstat -anp |grep 80
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3785/docker-proxy
tcp6       0      0 :::8080                 :::*                    LISTEN      3792/docker-proxy
unix  2      [ ACC ]     STREAM     LISTENING     57863    3808/containerd-shi  /run/containerd/s/2ec67cbd3cf0a5e172513d980e4ec1174e571a4b29c2b62d655411eb810fae87
unix  2      [ ACC ]     STREAM     LISTENING     29880    1327/containerd      /run/containerd/containerd.sock.ttrpc
unix  3      [ ]         STREAM     CONNECTED     30080    2683/gsd-keyboard
unix  3      [ ]         STREAM     CONNECTED     52010    3808/containerd-shi  /run/containerd/s/2ec67cbd3cf0a5e172513d980e4ec1174e571a4b29c2b62d655411eb810fae87
unix  2      [ ]         DGRAM                    8680     956/python2
unix  3      [ ]         STREAM     CONNECTED     62481    3808/containerd-shi
unix  3      [ ]         STREAM     CONNECTED     49380    2647/boltd

这是我重新创建一个新容器并复制配置文件后的access-test.log:

root@6f5dff3c80f6:/etc/nginx# cat /var/log/nginx/access-test.log
192.168.32.186 - - [21/Mar/2023:17:15:32 +0800] "GET /icons/blank.gif HTTP/1.1" 404 555 "http://192.168.39.7/quince-builds/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-" "-" 192.168.32.186 - "http"
192.168.32.186 - - [21/Mar/2023:17:15:32 +0800] "GET /icons/back.gif HTTP/1.1" 404 555 "http://192.168.39.7/quince-builds/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-" "-" 192.168.32.186 - "http"
192.168.32.186 - - [21/Mar/2023:17:15:32 +0800] "GET /icons/folder.gif HTTP/1.1" 404 555 "http://192.168.39.7/quince-builds/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" "-" "-" 192.168.32.186 - "http"

Nginx日志:

192.168.32.186 - - [21/Mar/2023:17:15:32 +0800] "GET /quince-builds/ HTTP/1.1" 200 2642 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.3
6" "-"
2023/03/21 17:15:32 [error] 61#61: *4 open() "/usr/share/icons/blank.gif" failed (2: No such file or directory), client: 192.168.32.186, server: 192.168.39.7, request: "GET /icons/blank.gif HTTP/1.1", ho
st: "192.168.39.7", referrer: "http://192.168.39.7/quince-builds/"
2023/03/21 17:15:32 [error] 61#61: *3 open() "/usr/share/icons/back.gif" failed (2: No such file or directory), client: 192.168.32.186, server: 192.168.39.7, request: "GET /icons/back.gif HTTP/1.1", host: "192.168.39.7", referrer: "http://192.168.39.7/quince-builds/"
2023/03/21 17:15:32 [error] 61#61: *4 open() "/usr/share/icons/folder.gif" failed (2: No such file or directory), client: 192.168.32.186, server: 192.168.39.7, request: "GET /icons/folder.gif HTTP/1.1", host: "192.168.39.7", referrer: "http://192.168.39.7/quince-builds/"

顺便说一下,我想知道这是否是docker网络问题,所以我尝试重新启动docker服务并重新启动linux主机。

6pp0gazn

6pp0gazn1#

好吧,我认为Nginx没有问题.当我试图使用另一台机器发送请求时,它会显示“欢迎使用Nginx”页面.所以在对我的谷歌浏览器进行大规模清理后,我可以成功地获得正确的页面!

相关问题