【问题标题】:I am getting 404 while tying to reach my Rest-API with Nginx reverse_proxy我在尝试使用 Nginx 反向代理访问我的 Rest-API 时收到 404
【发布时间】:2019-06-16 09:41:06
【问题描述】:

我的云服务器 8084 端口上运行了一个 Rest-API。当我尝试访问 mysitename:8084/swagger-ui.html 时,可以看到 Swagger 的 Api Documentation 页面。但是当我以 mysitename.com/api/swagger-ui.html 尝试此操作时,出现 404 错误。

我在 StackOverflow 上阅读了很多关于这个问题的类似问题,我尝试了不同的解决方案但我无法成功。

这是我的 nginx 配置文件

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

        root /var/www/mysitename.com/html/;
        index index.html index.htm index.nginx-debian.html;

        server_name mysitename.com www.mysitename.com;
}

server {

    listen 443;
    server_name mysitename.com www.mysitename.com;

    ssl_certificate           /etc/letsencrypt/live/mysitename.com/cert.pem;
    ssl_certificate_key       /etc/letsencrypt/live/mysitename.com/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

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

    location / {
      root /var/www/mysitename.com/html/;
      index index.html index.htm index.nginx-debian.html;
    }
}

server {

    listen 443;
    server_name mysitename.com www.mysitename.com;

    ssl_certificate           /etc/letsencrypt/live/mysitename.com/cert.pem;
    ssl_certificate_key       /etc/letsencrypt/live/mysitename.com/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    location /api/ {
      proxy_set_header        Host $host;
      proxy_pass              https://127.0.0.1:8084/;
      proxy_read_timeout      90;

      try_files $uri $uri/ =404;
    }
}

编辑

这是nginx -T 输出

/etc/nginx/sites-available$ sudo nginx -T
nginx: [warn] conflicting server name "mysitename.com" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "www.mysitename.com" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果你能帮助我,我将不胜感激。

【问题讨论】:

  • 您似乎有两个 server 块具有相同的 listenserver_name。我怀疑其中第一个正在处理请求,而带有反向代理的请求被忽略了。使用nginx -T(大写T)测试你的配置,查看Nginx正在读取的整个配置。
  • 嗨@RichardSmith,谢谢你的回答。看来您是对的,我在帖子中添加了 nginx -T 输出。你能帮我解决这个冲突吗?

标签: rest nginx nginx-location nginx-reverse-proxy


【解决方案1】:

配置检查确认Richard Smith 的评论。只是警告有两个服务器块具有相同的listenserver_name

1) 尝试将您的location /api/ {} 转移到第二个服务器块的location / {} 块下方。

2) 移除整个第三个服务器块

3) 如果您希望从 http 重定向到 https,请在侦听端口 80 的服务器块末尾添加 return 301 https://$host$request_uri;

4) 再次运行nginx -T 以检查错误。

5) 如果没有发现错误,则重新加载 Nginx。

这是建议的服务器块配置文件:

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

        root /var/www/mysitename.com/html/;
        index index.html index.htm index.nginx-debian.html;

        server_name mysitename.com www.mysitename.com;
        return 301 https://$host$request_uri;
}

server {

    listen 443;
    server_name mysitename.com www.mysitename.com;

    ssl_certificate           /etc/letsencrypt/live/mysitename.com/cert.pem;
    ssl_certificate_key       /etc/letsencrypt/live/mysitename.com/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

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

    location / {
      root /var/www/mysitename.com/html/;
      index index.html index.htm index.nginx-debian.html;
    }

    location /api/ {
      proxy_set_header        Host $host;
      proxy_pass              https://127.0.0.1:8084/;
      proxy_read_timeout      90;

      ## try_files $uri $uri/ =404;
    }
}

希望有帮助!

【讨论】:

  • 这解决了我的问题。非常感谢您的帮助:)
  • 两个服务器块都需要还是我可以只使用第一个并添加位置 /api/ 到它?
  • 嗨@kd12345,每个服务器块都有不同的用途。第一个用于通过端口 80 接受请求(仅 HTTP 请求),而第二个用于通过端口 443 接受请求(HTTPS 请求)。如果您选择只使用第一个,您的站点将只能通过以下方式访问:http://mysitename.comhttp://www.mysitename.com,如果有人尝试使用 https 访问您的站点,浏览器将显示错误。对于开发,单独使用端口 80 是可以的。上线时,建议启用端口 443 并从端口 80 自动重定向,以便访问者始终看到 https
  • @johnsing 感谢您的精彩解释,非常感谢。另一个问题,所以目前当我使用 http:// 在浏览器中打开我的 ipaddress 时,我被定向到 index.nginx-debian.html 而不是我的 / 在我的 nodejs 服务器中声明的路由(这没关系,我不介意)。但是当我尝试访问 ipaddress/api 时,我不断收到错误 404。如何更新上面的代码,以便我可以从 nginx 访问我的 nodejs /api/ 路由?
  • @johnsing 我的 API 只需要这个 IP 地址,而不是服务器和网页
猜你喜欢
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-31
  • 1970-01-01
  • 2010-10-19
  • 2015-11-29
相关资源
最近更新 更多