Use Nginx in vue.js to solve cross-domain issues
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.
