企业网站源码 asp,个人怎么做淘宝客网站,wordpress好玩,抖音代运营成功案例文章目录 1#xff0c;问题描述1.1#xff0c;当网页和后台是不同服务时会产生跨域问题1.2#xff0c;跨域问题 2#xff0c;nginx端口转发解决跨域问题2.1#xff0c;下载并安装nginx2.1.1#xff0c;解压后如下所示2.1.2#xff0c;进入解压目录后#xff0c;执行配置… 文章目录 1问题描述1.1当网页和后台是不同服务时会产生跨域问题1.2跨域问题 2nginx端口转发解决跨域问题2.1下载并安装nginx2.1.1解压后如下所示2.1.2进入解压目录后执行配置脚本 2.2编译安装2.3nginx使用2.3.1设置nginx开机自启动2.3.2修改配置文件配置端口转发2.3.3启动nginx2.3.4发现由于80端口被apache占用了nginx无法启动2.3.5直接关闭apache使用nginx托管静态网站代码 3更新动态IP到nginx配置文件 1问题描述
1.1当网页和后台是不同服务时会产生跨域问题
Access to XMLHttpRequest at ‘http://ubuntu:9607/login’ from origin ‘http://www.anweimian.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
1.2跨域问题
同源策略会导致跨域问题
所谓同源指的是三个相同。协议相同域名相同端口相同http://www.example.com/dir2/other.html同源 http://example.com/dir/other.html不同源域名不同 http://v2.www.example.com/dir/other.html不同源域名不同 http://www.example.com:81/dir/other.html不同源端口不同
2nginx端口转发解决跨域问题
2.1下载并安装nginx
https://nginx.org/en/download.html
2.1.1解压后如下所示 2.1.2进入解压目录后执行配置脚本
./configureHTTP rewrite 模块需要 PCRE 库的支持sudo apt-get install libpcre3-dev安装好PCRE 库后再次执行 ./configure2.2编译安装
sudo make -j
sudo make install2.3nginx使用
2.3.1设置nginx开机自启动
vim /etc/rc.local
文本底部追加
/usr/local/nginx/sbin/nginx2.3.2修改配置文件配置端口转发 /usr/local/nginx/conf/nginx.conf2.3.3启动nginx
sudo /usr/local/nginx/sbin/nginx2.3.4发现由于80端口被apache占用了nginx无法启动 2.3.5直接关闭apache使用nginx托管静态网站代码
sudo /etc/init.d/apache2 stop#重启nginx
sudo /usr/local/nginx/sbin/nginx -s reload3更新动态IP到nginx配置文件
#!/usr/bin/bash
# 为 win 设置 wsl host
# win hosts 文件路径
# 获取 wsl2 的 ip
wsl_ip$(ifconfig eth0 | grep -w inet | awk {print $2})CHANGE_HOST()
{HOST_NAME$1HOST_IP$2win_hosts_path/mnt/c/Windows/System32/drivers/etc/hosts# 判断是否已存在 wsl2 的域名如果存在则修改否则追加if grep -wq $HOST_NAME $win_hosts_paththen# 此处因为权限问题没有直接用 sed 修改 hosts 文件win_hosts$(sed -s s/.* $HOST_NAME/$HOST_IP $HOST_NAME/g $win_hosts_path)echo $win_hosts $win_hosts_pathelseecho $HOST_IP $HOST_NAME $win_hosts_pathfi
}CHANGE_NGINX_HOST_BY_PORT()
{SERVER_PORT$1HOST_IP$2nginx_config_path/usr/local/nginx/conf/nginx.conf# 判断是否已存在 wsl2 的域名如果存在则修改否则追加if grep -wq proxy_pass\|$SERVER_PORT $nginx_config_paththen# 此处因为权限问题没有直接用 sed 修改 hosts 文件nginx_conf$(sed -s s/.*proxy_pass.*http:\/\/.*:$SERVER_PORT;/ proxy_pass http:\/\/$HOST_IP:$SERVER_PORT/g $nginx_config_path)echo $nginx_conf $nginx_config_pathelseecho $SERVER_PORT is not in $nginx_conffi
}CHANGE_HOST www.anweimian.com $wsl_ip
CHANGE_HOST ubuntu $wsl_ipCHANGE_NGINX_HOST_BY_PORT 9607 $wsl_ip