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)

});

相关推荐
Csvn2 小时前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx
wno7043 小时前
Nginx代理MySQL或PG连接
运维·mysql·nginx
Csvn16 小时前
Nginx 性能调优与压测:连接账本、找拐点与调优前后对比
后端·nginx
Csvn2 天前
Nginx 与 OpenResty:阶段钩子、shared dict 与动态 upstream/证书
后端·nginx
Csvn3 天前
Nginx HTTPS 与安全加固:会话复用、OCSP、mTLS 与限流取舍
后端·nginx
jaysee-sjc4 天前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
Csvn4 天前
Nginx 日志、监控与排障:全字段日志、条件日志与证据链定位
后端·nginx
杨凯凡5 天前
Spring Boot SSE 上线后不实时、频繁断线?Nginx 缓冲、心跳与连接回收实战
spring boot·后端·nginx
Csvn5 天前
Nginx 缓存体系:三层 key、七态 X-Cache-Status 与锁/stale 策略
后端·nginx
Csvn6 天前
Nginx 负载均衡进阶与健康检查:哈希、keepalive 池与 DNS 自愈
后端·nginx