Below I will share with you an article about using vue.js proxy and using Nginx to solve cross-domain problems. It has a good reference value and I hope it will be helpful to everyone.
Use Nginx reverse proxy to solve cross-domain problems (vue.js uses a proxy to remove options requests triggered by vue.js due to cross-domain)
Our project still requires node.js as a container ’s
1. Install Nginx under Windows (Download the stable version from the official website http://nginx.org/en/download.html)
2. Modify the server of the nginx.conf file in the config
server { listen 8899;// 你的端口 server_name localhost; root C:/ZOBSF_F/dist;//你打包部署的文件路径 #charset koi8-r; #access_log logs/host.access.log main; # 匹配 api 路由的反向代理到API服务 location ^~/api { proxy_pass http://119.23.227.141:10001/;//你的后端IP和端口 } #根据路由设置,避免出现404 location / { try_files $uri $uri/ @router; index index.html; } location @router { rewrite ^.*$ /index.html last; } #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; #} }
3. The dist file packaged by your uve.js is as follows
## Fourth, then add a project startup configuration file server.production.js
var express = require('express'); var app = express(); var compression = require('compression'); var proxyMiddleware = require('http-proxy-middleware') var history = require('connect-history-api-fallback'); app.use(compression()); app.use(express.static(__dirname)); app.middleware = [//使用代理api proxyMiddleware(['/api'], {target: 'http://192.168.11.103:10001', changeOrigin: true, pathRewrite: { '^/api' : '/', },}), ]; app.get('*', function(req, res) { res.sendFile(__dirname + '/index.html'); }); app.use(history()); app.use(app.middleware); app.listen('8080', function(error) { console.info("==================系统正在启动中...============================="); if (error) { console.error(error) } else { console.info("==================9999系统启动成功!!!=============================") } });
5. Then use the command node server.production.js in the project directory and press Enter to find that the package lacks dependencies. Use npm install [dependency component of error message] to have express, compression, http-proxy-middleware etc.
Then start the project and access the address http://localhost:8080/xxxSix, vue.js uses proxy Specifically, you can refer to Baidu (just modify inde.js in the config configuration file)
proxyTable: { '/api': { target: Host.Host,//设置你调用的接口域名和端口号 别忘了加http changeOrigin: true, pathRewrite: { '^/api': '/'//这里理解成用‘/api'代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add'即可 } } },
How to implement asynchronous component loading in vue webpack?
Related to jsonp cross-domain issues in vue-resource
How to modify the sub-component style through the parent component in vue
The above is the detailed content of Use Nginx in vue.js to solve cross-domain issues. For more information, please follow other related articles on the PHP Chinese website!