nginx解决跨域问题
来源:互联网 发布:python的位运算作用 编辑:程序博客网 时间:2024/06/05 20:14
场景:通过localhost反向代理至本地tomcat中html页面,ajax调用第三方接口,产生跨域问题。
思路:
1.nginx和tomcat不能共用同一端口,url一样,端口不同,也是跨域。所以需要nginx将localhost代理到tomcat 8020端口。浏览器输入: http://localhost/web/login.html,实际访问:http://192.168.0.134:8020/web/html
2.html中调用接口代码如下:
$.ajax({
url:"http://localhost/api/login,
type:"get",
async: false,
data:{"loginName":loginName,"password":password,},
success:function(data){
alert(data);
}
});
根据配置,nginx将调用接口地址代理为:http://116.62.40.204:8080/api/login
以下是nginx.conf配置:
#user nobody;worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat {
server 192.168.0.134:8020;
}
upstream api {
server 116.62.40.204:8080;
}
server {
listen 80;
server_name 192.168.0.128;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat;
#expires 12h;
}
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://api;
#expires 12h;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
0 0
- nginx 解决跨域问题
- nginx解决跨域问题
- nginx解决跨域问题
- nginx解决跨域问题
- Nginx解决ajax跨域请求问题
- Nginx反向代理解决跨域问题
- Nginx解决跨域问题(CORS)
- Nginx转发地址解决跨域问题
- 利用Nginx解决跨域问题
- nginx 解决跨域和cookie问题
- nginx解决前端跨域问题
- nginx反向代理解决跨域问题
- nginx解决cookie跨域问题
- 解决同域Nginx ajax 请求跨域的问题
- nginx 修改头文件解决 字体跨域问题
- 前后台分离开发,nginx解决跨域问题
- 利用nginx 反向代理解决跨域问题
- 跨域问题可以通过配置nginx来解决
- [Hadoop]MapReduce多输出
- iOS端执行包含外部引用的js并相互通信
- 由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法及基础ssh知识理解
- 策略模式(Strategy)
- XAPI Host Plugins
- nginx解决跨域问题
- CircleTextView的实现
- 使用java 操作MongoDB 的增删操作
- 数据库Schema的优化
- 关于linux环境变量的设置
- javascript请求天气API大全
- 【Java集合系列】---ArrayList
- JavaScript location对象用法详解
- iperf局域网测速