nginx解决静态页面中ajax跨域方案配置

nginx 配置内容

server {

listen 9999;#监听端口

server_name localhost;#域名

add_header Access-Control-Allow-Origin *; # 允许跨域

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root D:/html/index.html;#静态html项目的地址

index index.html;

autoindex off;

}

location /test/ {

#以下为Ajax中跨域配置项

add_header 'Access-Control-Allow-Origin' '*';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

add_header 'Access-Control-Allow-Headers' '*';

add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

proxy_pass http://123.456.789.10:1234;# 测试请求地址为你项目具体请求地址

}

}

nginx 命令:

start nginx 启动nginx

nginx -s stop 快速关闭nginx

nginx -s reload 改变了Nginx相关配置,需要重新加载配置而重载。

html 页面请求

var getUrl='http://127.0.0.1:9999/test/net/testtarget'

$.get(getUrl,param,function(data,status){

console.log(data)

console.log(status)

});

相关推荐
HYI5 小时前
小公司前端多分支测试太痛苦?我自己写了个轻量 CLI
nginx·vite
timeweaver9 小时前
深度解析 Nginx 前端 location 配置与优先级:你真的用对了吗?
前端·nginx·前端工程化
Moment14 小时前
nginx 如何配置防止慢速攻击 🤔🤔🤔
前端·后端·nginx
伊成1 天前
Docker 部署 Nginx 完整指南
nginx·docker·容器
小白的代码日记2 天前
Nginx学习与安装
运维·nginx
ayaya_mana2 天前
Nginx性能优化与安全配置:打造高性能Web服务器
运维·nginx·安全·性能优化
油丶酸萝卜别吃2 天前
nginx配置代理服务器
运维·网络·nginx
花酒锄作田2 天前
Nginx反向代理Kafka集群
nginx·kafka
奥格列的魔法拖鞋~2 天前
Docker-LNMP架构 创建多项目- 单个ngixn代理多个PHP容器服务
nginx·docker·eureka·架构·php·lnmp
皓空揽月2 天前
php+apache+nginx 更换域名
nginx·php·apache