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)

});

相关推荐
tryCbest18 小时前
Nginx常用操作命令-Linux和Windows系统
linux·windows·nginx
難釋懷1 天前
Nginx实现本地缓存查询
nginx·spring·缓存
ZzzZZzzzZZZzzzz…1 天前
Nginx 平滑升级:从 1.26.3 到 1.28.0,用户无感知
linux·运维·nginx·平滑升级·nginx1.26.3·nginx1.28.0
robch2 天前
python3 -m http.server 8001直接启动web服务类似 nginx
前端·nginx·http
cyber_两只龙宝2 天前
【Nginx】Nginx实现FastCGI详解
linux·运维·nginx·云原生·php·memcached·fastcgi
BullSmall2 天前
Prometheus 可以监控docker 部署的Nginx 吗?
nginx·docker·prometheus
魔卡少女12 天前
Nginx配置代码化自动部署詹金斯/Github方案
前端·nginx·github
倔强的胖蚂蚁2 天前
AI 人工智能配置管理 Nginx
运维·nginx·云原生
邓霖涛2 天前
nginx使用openSSL自签生成https相关证书
服务器·nginx·https
xiaoyaohou112 天前
025、分布式计算实战:Spark Core与Spark SQL
sql·ajax·spark