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)

});

相关推荐
追夢秋陽31 分钟前
MacOS 通过Shell 脚本一步到位配置nginx+Php环境
nginx·macos·php·一站式配置
No8g攻城狮1 天前
【Nginx】Nginx实现动态路由映射
运维·nginx
unirst19850071 天前
使用vite打包并部署vue项目到nginx
前端·vue.js·nginx
市安1 天前
基于Debain构建Ngxin镜像
运维·nginx·docker·云原生·容器·debian·镜像
阿正的梦工坊1 天前
Nginx(发音:engine x)是什么?
运维·nginx
程序员洪志道2 天前
封装复杂性:一个反复生效的架构手法
nginx·程序员
Dear~yxy2 天前
Nginx知识点详解
运维·nginx
cc.ChenLy2 天前
Nginx核心解析:正向代理、反向代理、负载均衡、下载、安装、使用...
运维·nginx·负载均衡
FJW0208142 天前
Nginx + Redis + srcache + PHP-FPM架构部署
redis·nginx·php
codingWhat2 天前
手把手系列之——前端的Nginx 与 Docker 部署实践
nginx·docker