简体   繁体   English

我在与Nginx reverse_proxy达成我的Rest-API时得到404

[英]I am getting 404 while tying to reach my Rest-API with Nginx reverse_proxy

I have a Rest-API running on my cloud server 8084 port. 我在我的云服务器8084端口上运行了Rest-API。 When I try to access mysitename:8084/swagger-ui.html, I can see the Api Documentation page of Swagger. 当我尝试访问mysitename:8084 / swagger-ui.html时,我可以看到Swagger的Api文档页面。 But when I try this as mysitename.com/api/swagger-ui.html, I get a 404 error. 但当我尝试将其作为mysitename.com/api/swagger-ui.html时,我收到404错误。

I read a lot of similar questions about this problem on StackOverflow, I tried different solutions but I could not succeeded. 我在StackOverflow上阅读了很多关于这个问题的类似问题,我尝试了不同的解决方案但是我没能成功。

Here is my nginx config file 这是我的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;
    }
}

EDIT 编辑

This is nginx -T output 这是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

I'd appreciate it if you could help me. 如果你能帮助我,我会很感激的。

The configuration check confirms Richard Smith 's comment. 配置检查确认了理查德史密斯的评论。 Just a warning that there are two server blocks with the same listen and server_name . 只是警告说有两个服务器块具有相同的listenserver_name

1) Try to transfer your location /api/ {} right below your location / {} block of the 2nd server block. 1)尝试在第二个服务器块的location / {}块下方转移您的location /api/ {}

2) Remove the whole 3rd server block 2)删除整个第3个服务器块

3) If you wish to redirect from http to https, add return 301 https://$host$request_uri; 3)如果您希望从http重定向到https,请添加return 301 https://$host$request_uri; right at the end of your server block listening to port 80. 在服务器块的末尾听取端口80。

4) Run nginx -T again to check for errors. 4)再次运行nginx -T以检查错误。

5) Reload Nginx, if no errors were found. 5)如果没有发现错误,重新加载Nginx。

Here's the suggested server block configuration file: 这是建议的服务器块配置文件:

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

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM