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

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

說(shuō)明我的項(xiàng)目?jī)?nèi)接口是沒(méi)有問(wèn)題的。
再測(cè)試nginx代理后的接口:
再u(mài)buntu中輸入命令
curl http://localhost:8080/api/user/login
運(yùn)行結(jié)果:

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

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

至此,問(wèn)題完美解決。
到此這篇關(guān)于nginx訪問(wèn)動(dòng)態(tài)接口報(bào)錯(cuò)404Not Found問(wèn)題解決的文章就介紹到這了,更多相關(guān)nginx訪問(wèn)動(dòng)態(tài)接口報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx 配置 ModSecurity 網(wǎng)絡(luò)應(yīng)用防火墻實(shí)現(xiàn)
這篇文章主要介紹了Nginx 配置 ModSecurity 網(wǎng)絡(luò)應(yīng)用防火墻實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Nginx 反向代理與負(fù)載均衡運(yùn)行小結(jié)
Nginx還支持對(duì)后端服務(wù)器進(jìn)行健康檢查,當(dāng)某個(gè)服務(wù)器不可用時(shí),Nginx會(huì)自動(dòng)將流量重定向到其他可用的服務(wù)器,這篇文章給大家分享Nginx 反向代理與負(fù)載均衡是如何運(yùn)行的,感興趣的朋友一起看看吧2024-03-03
基于Nginx+lua實(shí)現(xiàn)簡(jiǎn)單的XSS攻擊攔截
WAF即web應(yīng)用防火墻,Nginx是一個(gè)主流的代理服務(wù),除了本身的Nginx日志,作為用戶(hù)肯定也支持對(duì)請(qǐng)求信息進(jìn)行操作,?很多都是通過(guò)在代理服務(wù)器上掛載規(guī)則特征,實(shí)現(xiàn)軟件層面的軟WAF進(jìn)行WEB防護(hù),本文主要給大家介紹了Nginx+Lua實(shí)現(xiàn)一個(gè)簡(jiǎn)單的XSS攻擊攔截,需要的朋友可以參考下2024-01-01
在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)流媒體的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-06-06

