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)

});

相关推荐
GDAL13 小时前
NJS 共享字典(ngx.shared)全解析:跨 Worker 进程的数据共享方案
nginx·shared·njs
xifangge202513 小时前
PHP 错误日志在哪里看?Apache / Nginx / PHP-FPM 一次讲清
nginx·php·apache
鸠摩智首席音效师13 小时前
如何安装和配置 Nginx 反向代理服务器 ?
运维·nginx
星光不问赶路人15 小时前
Nginx 的 location 路径匹配语法详解
nginx·api
GDAL15 小时前
深入理解 NJS 全局对象:掌控运行时的核心工具
nginx·njs
GDAL17 小时前
精通 NJS HTTP 请求对象:全方位掌控 NGINX 请求生命周期
nginx·njs
飞翔沫沫情17 小时前
Nginx运维维护规范及全配置详解【持续更新】
nginx·nginx 配置·nginx 操作手册·nginx 使用规范·nginx 日志规范·nginx 配置文件说明
deriva18 小时前
nginx如何将某域名/二级站点/代理到二级站点?以ChirpStack实战为例
运维·nginx
睡不醒的猪儿1 天前
nginx常见的优化配置
运维·nginx
qq_406176141 天前
关于JavaScript中的filter方法
开发语言·前端·javascript·ajax·原型模式