我在尝试使用 Nginx reverse_proxy 访问我的 Rest-API 时收到 404
I am getting 404 while tying to reach my Rest-API with Nginx reverse_proxy
我的云服务器8084端口上有一个Rest-API 运行。当我尝试访问 mysitename:8084/swagger-ui.html 时,我可以看到 Swagger 的 Api 文档页面。但是,当我尝试使用 mysitename.com/api/swagger-ui.html 时,出现 404 错误。
我在 Whosebug 上阅读了很多关于这个问题的类似问题,我尝试了不同的解决方案但我无法成功。
这是我的 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
如果你能帮助我,我将不胜感激。
配置检查确认 Richard Smith 的评论。只是警告有两个服务器块具有相同的 listen
和 server_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;
}
}
希望对您有所帮助!
我的云服务器8084端口上有一个Rest-API 运行。当我尝试访问 mysitename:8084/swagger-ui.html 时,我可以看到 Swagger 的 Api 文档页面。但是,当我尝试使用 mysitename.com/api/swagger-ui.html 时,出现 404 错误。
我在 Whosebug 上阅读了很多关于这个问题的类似问题,我尝试了不同的解决方案但我无法成功。
这是我的 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
如果你能帮助我,我将不胜感激。
配置检查确认 Richard Smith 的评论。只是警告有两个服务器块具有相同的 listen
和 server_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;
}
}
希望对您有所帮助!