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)

});

相关推荐
HEADKON11 小时前
培西达替尼Pexidartinib对比伊马替尼在腱鞘巨细胞瘤中的CSF1R抑制与肝毒性风险
nginx
燕-孑11 小时前
Nginx详解——进阶
运维·nginx
野熊佩骑14 小时前
一文读懂Nginx 之 Ubuntu使用apt方式安装Nginx官方最新版本
linux·运维·服务器·nginx·ubuntu·http
墨着染霜华15 小时前
Windows 启动 Nginx 一闪而过、pid 丢失、logs 目录报错彻底解决
运维·windows·nginx
ji_shuke16 小时前
前端请求/authapi/auth/permissions 实际发给后端 /api/auth/permissions 本地和线上配置
运维·前端·nginx
赵成ccc16 小时前
【无标题】
linux·nginx
江上清风山间明月18 小时前
Nginx一个IP部署多个网站
运维·tcp/ip·nginx·部署多个网站·一个ip·一个服务器
文青小兵18 小时前
云计算Linux——数据库MySQL部分命令 (十五)
linux·数据库·mysql·nginx·云计算
文青小兵19 小时前
云计算Linux——负载均衡 (十四)
linux·运维·服务器·nginx·云计算·负载均衡
落叶_Jim1 天前
2026年Nginx配置HTTPS全流程-从零到自动续期实战指南
运维·nginx·https