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)

});

相关推荐
charlee446 小时前
nginx部署发布Vite项目
nginx·性能优化·https·部署·vite
CRMEB定制开发8 小时前
【实战】CRMEB Pro 企业版安装教程(附 Nginx 反向代理配置 + 常见问题解决)
nginx·商城系统·微信商城·crmeb
孙克旭_1 天前
day045-nginx跳转功能补充与https
linux·运维·nginx·https
博同学1 天前
Nginx + ELK + Grafana 全球访问热力图
nginx·elk·grafana
RW~1 天前
Minio安装配置,桶权限设置,nginx代理 https minio
运维·nginx·https·minio
CodeWithMe1 天前
Nginx入门进阶:从零到高手的实战指南
运维·nginx
Ahlson1 天前
【fnNAS】docker的nginx配置html
nginx·docker·容器·fnnas
斯普信专业组2 天前
K8s环境下基于Nginx WebDAV与TLS/SSL的文件上传下载部署指南
nginx·kubernetes·ssl
亦舒.2 天前
宝塔面板Nginx手动配置负载均衡实战指南
运维·nginx·负载均衡
deming_su2 天前
轻松上手:使用Nginx实现高效负载均衡
运维·nginx·负载均衡