nginx的location配置導致網(wǎng)關返回404問題
nginx的location配置導致網(wǎng)關返回404
再項目中使用nginx轉發(fā)到網(wǎng)關時,發(fā)現(xiàn)返回了404.
{
"timestamp": "2023-11-01T02:12:48.788+00:00",
"path": "http://core-manage-web/core/core-manage/servers/findServers",
"status": 404,
"error": "Not Found",
"message": null,
"requestId": "642b125b"
}從這個返回來看,應該是網(wǎng)關返回的信息。因為如果是nginx返回404的話,應該是返回的404.html才對。
所以看看出是網(wǎng)關找不到轉發(fā)的路徑。 從 "path": "//core-manage-web/core/core-manage/servers/findServers",看出我們的接口經(jīng)過nginx轉發(fā)之后,居然只有兩個//。正常應該只有一個才對。
再看一下location配置:
location /core {
proxy_pass http://gateway-upstream/;
}由于第一次使用使用nginx,所以對于這些配置還不是很了解。這上面的的 “location /core”標識路徑前綴匹配。 而我的 “http://core-gateway-upstream/”是以“/”結尾,這表示會將 location的匹配路徑(/core)替換掉在轉發(fā)。
gateway-upstream配置如下
upstream gateway-upstream {
server 192.168.111.1:10006;
}所以如果我是用 http:localhost:8080/core/login/xxxxx訪問,則經(jīng)過nginx轉發(fā)之后會變成192.168.111.1:10006//login/xxxxx
所以需要將 location /core改成 location /core/。即可:
location /core/ {
proxy_pass http://gateway-upstream/;
}nginx配置多個location訪問報404
解決方法
在自己配置的location中不要使用root配置文件目錄,替換為alias即可
完整配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
autoindex on; #開啟索引功能
autoindex_exact_size off; # 關閉計算文件確切大小(單位bytes),只顯示大概大小(單位kb、mb、gb)
autoindex_localtime on; # 顯示本機時間而非 GMT 時間
charset utf-8; # 避免中文亂碼
root file;
#index index.html index.htm;
}
location /file {
alias html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
nginx提示:500 Internal Server Error錯誤的解決方法
本文章來給大家總結了大量關于導致nginx中提示500 Internal Server Error錯誤的原因總結與解決方法分析有需要了解的朋友可參考參考2013-04-04
Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持
這篇文章主要介紹了Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持,使用Nginx運行ThinkPHP的必備配置,需要的朋友可以參考下2015-07-07

