nginx訪問動態接口報錯404Not?Found如何解決

問題描述

計設使用ant design vue和jfinal框架分別開發前端和后端,創建了一套招聘背調系統。嘗試將項目部署到服務器上,但在外部訪問時持續出現 404 not found 錯誤

nginx訪問動態接口報錯404Not?Found如何解決

解決思路

把錯誤定位為:找不到動態接口,但是不知道是項目中出現了問題,還是經nginx代理后出現了問題。
因此要分別測試 項目本身的接口 和 nginx代理后的接口。

首先測試項目內接口:

ubuntu端輸入命令:cURL http://localhost:port/xxx/xxx

這里我的接口是: curl http://localhost:20294/sys/login

運行結果:

nginx訪問動態接口報錯404Not?Found如何解決

說明我的項目內接口是沒有問題的。

再測試nginx代理后的接口:

再ubuntu中輸入命令

curl?http://localhost:8080/api/user/login

運行結果:

nginx訪問動態接口報錯404Not?Found如何解決

這里提示找不到接口了,說明問題出在代理服務器nginx上,所以我們要去修改nginx的配置文件。

按照其他博客的建議,我將nginx配置中這個地方加上了斜杠

nginx訪問動態接口報錯404Not?Found如何解決

重啟服務器后,還是不行。

徹底解決

在當我不知道怎么辦的時候,我突然發現我的服務器中,有兩個nginx·····
我在想是不是因為有兩個nginx,修改的配置文件不是我啟動的那個nginx。于是我把所有nginx配置文件都替換成我原始的配置文件,再重啟。還是不行

擔心是兩個nginx的問題,我把服務器中的所有nginx刪除了。刪除步驟(依次運行下面的步驟):

ps?aux|grep?nginx??#查看nginx進程 kill?-9?進程號??????#殺死上一步中查詢到的nginx(進程號在第二列) find?/?-name?nginx?#找到nginx的文件地址 rm?-rf?xxx?????????#刪除nginx所有文件

最后用weget安裝新的nginx,然后按照原本安裝步驟進行安裝,修改配置文件后,再運行curl訪問動態接口,突然就可以了!

下面貼出我的nginx配置文件:

user?root; #user??nobody; worker_processes??4;  #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"';  ????server_names_hash_bucket_size?128; ????client_header_buffer_size?32k; ????underscores_in_headers?on; ????large_client_header_buffers?4?32k; ????client_max_body_size?50m; ????#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;  ????#keepalive_timeout??0; ????keepalive_timeout??65; ????tcp_nopush??????????on; ????fastcgi_connect_timeout?300; ????fastcgi_send_timeout????300; ????fastcgi_read_timeout????300; ????fastcgi_buffer_size?????64k; ????fastcgi_buffers?????4???64k; ????fastcgi_busy_buffers_size?128k; ????fastcgi_temp_file_write_size?256k; ????tcp_nodelay?????????on;  ????#gzip??on;   ????###################################################### ????#############?????麻雀配置地址????########### ????###################################################### ????server?{ ????????listen???????8080; ????????server_name??somename;  ????????location?/api/?{ ????????????proxy_pass?http://0.0.0.0:20294/;?#映射到本地端口。 ????????????proxy_redirect?off; ????????????proxy_set_header?Host?$http_host; ????????????proxy_set_header?X-Real-IP?$remote_addr; ????????????proxy_set_header?X-Forwarded-Proto?https; ????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for; ????????????client_max_body_size?200m; ????????????proxy_connect_timeout?600; ????????????proxy_read_timeout?600; ????????} ???????? ????????location?/?{ ????????????root?/root/project-template/config/static; ????????????try_files?$uri?$uri/?@router; ????????????index?index.html; ????????????proxy_set_header?X-Real-IP?$remote_addr; ????????????proxy_set_header?X-Forwarded-Proto?https; ????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for; ????????????client_max_body_size?200m; ????????????proxy_connect_timeout?600; ????????????proxy_read_timeout?600; ????????}  ????????location?@router?{ ????????????rewrite?^.*$?/index.html?last; ????????} ????} }

需要注意的是:配置動態訪問api的時候,記得在最后加上斜杠

nginx訪問動態接口報錯404Not?Found如何解決

? 版權聲明
THE END
喜歡就支持一下吧
點贊9 分享