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)

});

相关推荐
小鱼小鱼.oO9 小时前
阿里云服务器安装nginx并配置前端资源路径(前后端部署到一台服务器并成功访问)
服务器·nginx·阿里云
广东数字化转型12 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
�FENG21 小时前
LVS、NGINX、HAPROXY的调度算法
nginx·lvs·haproxy·调度算法
余渔鱼11231 天前
ajax学习手册
学习·ajax·okhttp
哈哈哈哈哈哈哈哈哈...........1 天前
【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制
tcp/ip·nginx·lua
Adorable老犀牛1 天前
负载均衡将https请求转发后端http服务报错:The plain HTTP request was sent to HTTPS port
nginx·http·https·负载均衡
�FENG2 天前
Nginx+Tomcat负载均衡与动静分离架构
nginx·tomcat·负载均衡·动静分离
别骂我h2 天前
Nginx+Tomcat负载均衡群集
nginx·tomcat·负载均衡
結城2 天前
使用nginx配置反向代理,负载均衡
运维·服务器·nginx
恋上钢琴的虫2 天前
搭建nginx的负载均衡
运维·nginx·负载均衡