nginx訪問動態(tài)接口報錯404Not Found問題解決
問題描述
計設(shè)做了一套招聘背調(diào)系統(tǒng),前后端分別使用了Ant Design Vue與JFinal框架。想要將項目部署到服務(wù)器上,但是外部訪問一直報錯404Not Found

解決思路
把錯誤定位為:找不到動態(tài)接口,但是不知道是項目中出現(xiàn)了問題,還是經(jīng)nginx代理后出現(xiàn)了問題。
因此要分別測試 項目本身的接口 和 nginx代理后的接口。
首先測試項目內(nèi)接口:
在ubuntu端輸入命令:curl http://localhost:port/xxx/xxx
這里我的接口是: curl http://localhost:20294/sys/login
運行結(jié)果:

說明我的項目內(nèi)接口是沒有問題的。
再測試nginx代理后的接口:
再ubuntu中輸入命令
curl http://localhost:8080/api/user/login
運行結(jié)果:

這里提示找不到接口了,說明問題出在代理服務(wù)器nginx上,所以我們要去修改nginx的配置文件。
按照其他博客的建議,我將nginx配置中這個地方加上了斜杠

重啟服務(wù)器后,還是不行。
徹底解決
在當(dāng)我不知道怎么辦的時候,我突然發(fā)現(xiàn)我的服務(wù)器中,有兩個nginx·····
我在想是不是因為有兩個nginx,修改的配置文件不是我啟動的那個nginx。于是我把所有nginx配置文件都替換成我原始的配置文件,再重啟。還是不行
擔(dān)心是兩個nginx的問題,我把服務(wù)器中的所有nginx刪除了。刪除步驟(依次運行下面的步驟):
ps aux|grep nginx #查看nginx進(jìn)程 kill -9 進(jìn)程號 #殺死上一步中查詢到的nginx(進(jìn)程號在第二列) find / -name nginx #找到nginx的文件地址 rm -rf xxx #刪除nginx所有文件
最后用weget安裝新的nginx,然后按照原本安裝步驟進(jìn)行安裝,修改配置文件后,再運行curl訪問動態(tài)接口,突然就可以了!
下面貼出我的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;
}
}
}
需要注意的是:配置動態(tài)訪問api的時候,記得在最后加上斜杠

至此,問題完美解決。
到此這篇關(guān)于nginx訪問動態(tài)接口報錯404Not Found問題解決的文章就介紹到這了,更多相關(guān)nginx訪問動態(tài)接口報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx 配置 ModSecurity 網(wǎng)絡(luò)應(yīng)用防火墻實現(xiàn)
這篇文章主要介紹了Nginx 配置 ModSecurity 網(wǎng)絡(luò)應(yīng)用防火墻實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
在Linux系統(tǒng)中將Redmine和SVN整合入Nginx的方法
這篇文章主要介紹了在Linux系統(tǒng)中將Redmine和SVN整合入Nginx的方法,示例基于CentOS系統(tǒng),并用到了Ruby腳本,需要的朋友可以參考下2015-06-06
nginx?搭建http-flv(rtmp)流媒體的方法步驟
本文主要介紹了nginx?搭建http-flv(rtmp)流媒體的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-06-06

