我想通过Nginx reverse_proxy到达Rest-API时得到404

时间:2019-06-15 20:17:06

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

我的云服务器8084端口上运行着Rest-API。当我尝试访问mysitename:8084 / swagger-ui.html时,可以看到Swagger的Api文档页面。但是当我将其作为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

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

1 个答案:

答案 0 :(得分:0)

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

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

2)删除整个第3个服务器块

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;
    }
}

希望有帮助!