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:\XZY\importData; // 本地文件的路径
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 即可让改动生效