前端本地配置nginx解决跨域问题 - Go语言中文社区

前端本地配置nginx解决跨域问题


nginx解决跨域问题

  • 前几天项目经理告诉我,需要我完成一项任务,任务内容是根据他提供的Excel表格内容,写一段js调用后台接口创建标签分组创建分组及调用创建标签接口创建对应标签分组下面的标签。结果跨域了。

1.下载nginx

http://nginx.org/en/download.html
在这里插入图片描述

2.启动nginx

有很多种方法启动nginx

(1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过

(2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可

3.检查nginx是否启动成功

直接在浏览器地址栏输入网址 http://localhost:80,回车,出现以下页面说明启动成功

在这里插入图片描述

4.关闭nginx

如果使用cmd命令窗口启动nginx,关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx

(1)输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)

(2)使用taskkill taskkill /f /t /im nginx.exe

5、修改配置

  • localhost:8089访问你的本地资源
server {
        listen       8090; // 监听的端口
        server_name   localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   F:XZYimportData; // 本地文件的路径
            index  index.html index.htm; // 访问index首页的文件
        }

	location /v1/ { // nginx会将带/v1/请求转发到https://api-dmp.swellfun.com
	    # rewrite  ^/v1/(.*)$ /$1 break;
	    # proxy_set_header Host $host;
        # proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_pass https://api-dmp.swellfun.com;  // 这里填上服务器地址(你要nginx替你转发的接口地址)
        }

        #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;
        #}
    }

  • 当我们修改了nginx的配置文件nginx.conf 时,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload 即可让改动生效
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_41552521/article/details/95960491
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢