Home  >  Article  >  Operation and Maintenance  >  How nginx solves cross-domain issues

How nginx solves cross-domain issues

(*-*)浩
(*-*)浩Original
2019-07-13 15:15:064100browse

Separate front-end and back-end, use nginx to solve cross-domain problems

How nginx solves cross-domain issues

Front-end: vue.js nodejs webpack

Backend: SpringBoot

Reverse proxy server: nginx

Idea: Package the front-end code, let nginx point to static resources, and nginx forwards background requests.

1. Package the front-end code:

npm run build

will generate a dist folder. Contains an index.html file and a static folder. Take my local path as an example:

/Users/xxx/ideaProjects/webtest/dist

2. Open

nginx.conf in the /usr/local/etc/nginx directory, add the following to the server:

listen       80; #原为8080,避免冲突,更改为80
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
            root   /Users/xxx/ideaProjects/webtest/dist;
            index  index.html;
            
            # 此处用于处理 Vue、Angular、React 使用H5 的 History时 重写的问题
            if (!-e $request_filename) {
                rewrite ^(.*) /index.html last;
                break;
            }
        }


        # 代理服务端接口
        location /api/ {
            proxy_pass http://localhost:8080/;# 代理接口地址
        }

Test

The front-end sends a request: http://localhost /test, vue-router redirects it to http://localhost/api/demo/1, and the actual access is http://localhost:8080/demo/1.

Send a request directly to the background: visit http://localhost/api/demo/1, the actual visit is: http://localhost:8080/demo/1

More Nginx related For technical articles, please visit the Nginx Tutorial column to learn!

The above is the detailed content of How nginx solves cross-domain issues. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn