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)

});

相关推荐
尘下吹霜20 分钟前
【记录】Nginx日志过大导致服务器空间爆满
服务器·网络·nginx
我要打倒凹凸曼1 小时前
使用 Nginx 实现 Tomcat 集群的负载均衡
nginx·tomcat·负载均衡
saadiya~2 小时前
实战笔记:在 Ubuntu 离线部署 Vue + Nginx 踩坑与避雷指南
vue.js·笔记·nginx
chilavert31819 小时前
技术演进中的开发沉思-278 AJax :Rich Text(上)
前端·javascript·ajax
秋雨雁南飞20 小时前
Nginx安装
nginx
全栈工程师修炼指南1 天前
Nginx | ngx_cache_purge 模块:实现清除特定上游服务(后端)响应缓存条目
运维·nginx·缓存
chilavert3181 天前
技术演进中的开发沉思-279 AJax :Rich Text Editor(下)
前端·javascript·ajax
irisart1 天前
第二章【NGINX 开源功能】—— 四层反向代理
运维·nginx·开源
MACKEI1 天前
图片加速效果验证与查看
nginx
qinyia1 天前
WisdomSSH解决Ubuntu 25.04服务器上因dpkg中断导致的Nginx安装失败问题并成功部署科技感个人主页
服务器·nginx·ubuntu