vue項(xiàng)目本地開(kāi)發(fā)使用Nginx配置代理后端接口問(wèn)題
使用Nginx配置代理后端接口
nginx的nginx.conf文件配置如下:
#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 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:9528;
}
location /dev/api/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS,PUT,DELETE';
add_header Access-Control-Allow-Headers 'Token,DNT,X-Mx-ReqToken,keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_pass https://www.xxxx:5002/api/; #這里填上服務(wù)器地址
}
#location / {
# try_files $uri $uri/ /index.html;
#}
#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;
# }
#}
}
然后瀏覽器輸入http://localhost:8088 打開(kāi)項(xiàng)目,看下接口是否調(diào)成功;
關(guān)于Nginx代理前后端接口
最近在開(kāi)發(fā)公司的前后端分離的項(xiàng)目,前端使用Vue,后端使用SpringBoot,本來(lái)我是想用Vue的代理功能,去請(qǐng)求后端接口,但是不知道死活代理不同。
proxy: {
? ? '/api': {
?? ? ?? ?target: 'http://127.0.0.1:8981/api/',
? ?? ??? ?pathRewrite: {
?? ? ? ?'^/api': '/'
? ?}
}這樣的寫(xiě)法各種出現(xiàn)問(wèn)題,比如404等問(wèn)題。
后來(lái)問(wèn)了一下我們公司的老員工,他說(shuō)現(xiàn)在不建議這么配。直接用Nginx代理前后端就好。
# 代理前端的地址
location /data-pages {
?? ?proxy_pass ?http://localhost:8021/data-pages/;
}
# 代理后端地址
location /api/ {
?? ?proxy_pass http://localhost:28090/api/;
}之前我想的是,Vue用的是8021端口,所以后端請(qǐng)求過(guò)去就會(huì)變成 http://localhost:8021/api/,但是其實(shí)不是這樣的。后端地址是看瀏覽器中的url然后拼接過(guò)去的。所以經(jīng)過(guò)Nginx代理之后,后端的接口也能通過(guò)Nginx代理,這樣就能解決問(wèn)題。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ)
本文主要介紹了element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Vue?ELement?Table技巧表格業(yè)務(wù)需求實(shí)戰(zhàn)示例
這篇文章主要介紹了Vue?ELement?Table技巧表格業(yè)務(wù)需求實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Vue3中reactive丟失響應(yīng)式的問(wèn)題解決(避大坑!)
這篇文章主要給大家介紹了關(guān)于Vue3中reactive丟失響應(yīng)式的問(wèn)題解決,vue3中reactive定義的引用類(lèi)型直接賦值導(dǎo)致數(shù)據(jù)失去響應(yīng)式 ,需要的朋友可以參考下2023-07-07
在vue-cli項(xiàng)目中使用bootstrap的方法示例
本篇文章主要介紹了在vue-cli項(xiàng)目中使用bootstrap的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Vue使用moment將GMT時(shí)間轉(zhuǎn)換為北京時(shí)間
GMT(Greenwich Mean Time)和UTC(Coordinated Universal Time)是兩個(gè)時(shí)間標(biāo)準(zhǔn),它們?cè)谠S多方面非常相似,但也有一些微小的差異,本文給大家詳細(xì)介紹了Vue使用moment將GMT時(shí)間轉(zhuǎn)換為北京時(shí)間,需要的朋友可以參考下2023-12-12
詳解TypeScript+Vue 插件 vue-class-component的使用總結(jié)
這篇文章主要介紹了TypeScript+Vue 插件 vue-class-component的使用總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Vue 項(xiàng)目中如何使用fullcalendar 時(shí)間段選擇插件(類(lèi)似課程表格)
最近完成一個(gè)項(xiàng)目,需要選擇一個(gè)會(huì)議室,但是最好能夠通過(guò)在圖上顯示出該 會(huì)議室在某某時(shí)間段內(nèi)已經(jīng)被預(yù)定了,初看這個(gè)功能感覺(jué)很棘手,仔細(xì)分析下實(shí)現(xiàn)起來(lái)還是挺容易的,下面通過(guò)示例代碼講解Vue項(xiàng)目中使用fullcalendar時(shí)間段選擇插件,感興趣的朋友一起看看吧2024-07-07
Vue通過(guò)字符串關(guān)鍵字符實(shí)現(xiàn)動(dòng)態(tài)渲染input輸入框
這篇文章主要為大家詳細(xì)介紹了Vue如何通過(guò)字符串關(guān)鍵字符實(shí)現(xiàn)動(dòng)態(tài)渲染input輸入框。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12

