【问题标题】:redirection with nginx is not working.使用 nginx 重定向不起作用。
【发布时间】:2018-07-02 01:26:19
【问题描述】:

我仍在学习 nginx 并尝试使用 nginx 为我的 Spring Boot 应用程序进行简单的重定向。我已经设法使用 docker 设置了所有内容,这是我的 docker 文件和我的 default.conf 文件。

DockerFile

  RUN apt-get update -y && apt-get install -y nginx
  RUN rm -f /etc/nginx/sites-enabled/default
  ADD default.conf /etc/nginx/conf.d

/etc/nginx/conf.d 中的default.conf 文件

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

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
     proxy_set_header X-Forwarded-Proto http;
     proxy_pass http://localhost:8080/testing;
    }
  }

我正在执行 Dockerfile 中的 .sh 文件,该文件将启动 nginx 服务并启动 jar 文件。

我不知道我在这里做错了什么,是我的配置错误还是我应该删除 nginx.conf 文件并将其内容替换为 default.conf 内容.我也尝试用 localhost 替换 default_server 但没有任何帮助。我的 Spring Boot 应用程序在端口 8080 上运行。

【问题讨论】:

  • 在这种情况下为什么需要nginx?如果您真的需要它,“Docker 方式”强制您将 nginxspring application 移动到单独的容器中。

标签: docker spring-boot nginx dockerfile load-balancing


【解决方案1】:

localhost 在您的 docker 容器(运行 nginx 的地方)中不是指向您主机的环回接口,而是指向容器的环回设备。您需要更改 proxy_pass 配置才能使用主机的 IP 地址(另外,确保您的 Spring Boot 应用程序正在侦听 0.0.0.0 而不仅仅是 localhost)。

【讨论】:

  • 所以你说的是 server_name 应该是我的机器 ip 和 proxy pass 应该是这样的。 proxy_pass http://$host:8080/testing;
  • 不确定最后的/testing(取决于您的sping启动应用程序),但如果设置为localhost,主机名显然是错误的。
  • 在删除 server_name 并将 proxy_pass 替换为 proxy_pass http://127:0:0:1:8080/testing 后,我能够解决这个问题。但我不能在这一行添加$host。你能告诉mw为什么吗?
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 2019-04-29
  • 2020-07-18
  • 2022-01-14
  • 2017-10-27
  • 1970-01-01
  • 2014-08-14
  • 2018-09-15
相关资源
最近更新 更多