前端跨域配置本地nginx代理

797 阅读2分钟

前端跨域配置本地nginx代理

1, 本地前端浏览器打开地址http://localhost:8080

2, nginx配置监听地址http://localhost:8088

3, 后台请求接口地址http://ip:8090/hip-dspool

1. 安装nginx

地址:nginx.org/en/download…

选个稳定版本

2. 解压后

image-20230510135339237.png 执行nginx.exe的,启动后会有一闪而过的状态,

然后在浏览器输入localhost会显示如下页面,表示启动成功了

image-20230510140828210.png

强制杀掉nginx进程

taskkill /f /t /im nginx.exe

image-20230510140943958.png

3. 原本项目请求

项目浏览器打开页面http://localhost:8080/#,中间请求后台地址跨域了

image-20230510141411268.png

4. 配置nginx代理

image-20230510142122715.png

​
​
#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 / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://localhost:8080;
        }
        
        location /hip-dspool/ {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            if ($request_method = 'OPTIONS') {
                return 204;
            }
            # root   html;
            # index  index.html index.htm;
            proxy_pass http://ip:8090;
        }
​
        #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;
    #    }
    #}
​
}
​

如上配置好之后,就可以请求了

5. 修改项目请求

将请求后台地址的IP和端口改成nginx的如上就是http://localhost:8088

image-20230510142437695.png

6. 成功啦

image-20230510142742469.png

7. 跨域问题

如果后台也同时设置了跨域处理Access-Control-Allow-Origin:*,

但是前端请求又要加上withCredentials:true,添加凭证cookie等信息 就得设置 Access-Control-Allow-Credentials' true;

浏览器请求接口会产生跨域问题,如下

image.png

所以:我们可以设置nginx代理, 其中反向代理proxy_hide_header 可以设置隐藏掉主机信息 (Nginx中proxy_hide_header 与fastcgi_hide_header都可以隐藏主机头信息,proxy_hide_header在ngx_http_proxy_module下,fastcgi_hide_header在ngx_http_fastcgi_module模块下, 当nginx作为反向代理时,想要隐藏后端webserver主机信息的时候,可以使用proxy_hide_header来屏蔽后端主机信息, 当nginx作为webserver时,使用fastcgi_hide_header来屏蔽当前主机信息)

然后设置指定的域名进行访问,和设置允许跨域请求携带凭证信息,如下图:

image.png

image.png