当前位置: 首页 > news >正文

nginx配置跨域请求,后台不用配置啦,完美

允许全部把域名改*

server {  listen       22222;  server_name  localhost;  location  / {  if ($request_method = 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:8080';  add_header Access-Control-Allow-Headers '*';  add_header Access-Control-Allow-Methods '*';  add_header Access-Control-Allow-Credentials 'true';  return 204;  }  if ($request_method != 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:8080' always;  add_header Access-Control-Allow-Credentials 'true';  }  proxy_pass  http://localhost:59200;   }  
}  

或者

server {  listen       22222;  server_name  localhost;  location  / {  add_header Access-Control-Allow-Origin 'http://localhost:8080' always;  add_header Access-Control-Allow-Headers '*';  add_header Access-Control-Allow-Methods '*';  add_header Access-Control-Allow-Credentials 'true';  if ($request_method = 'OPTIONS') {  return 204;  }  proxy_pass  http://localhost:59200;   }  
}  

完整配置,有lua


#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;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;server {listen       80;server_name  localhost;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# 处理以 /api 开头的请求location ^~ /main {if ($request_method = 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173';  add_header Access-Control-Allow-Headers '*';  add_header Access-Control-Allow-Methods '*';  add_header Access-Control-Allow-Credentials 'true';  return 204;  }  if ($request_method != 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  add_header Access-Control-Allow-Credentials 'true';  }access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";# 将 /main 替换为空,只保留后续的路径rewrite ^/main(.*)$ $1 break;proxy_pass http://127.0.0.1:9001;# proxy_pass http://127.0.0.1:42002;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}# 处理 /wbrjPys/static 的请求location ^~ /wbrjPys/static {rewrite ^/wbrjPys(.*)$ $1 break;proxy_pass http://127.0.0.1:9090;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location ^~ /wbrjPys {if ($request_method = 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173';  add_header Access-Control-Allow-Headers '*';  add_header Access-Control-Allow-Methods '*';  add_header Access-Control-Allow-Credentials 'true';  return 204;  }  if ($request_method != 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  add_header Access-Control-Allow-Credentials 'true';  } access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";# 将 /api 替换为空,只保留后续的路径rewrite ^/wbrjPys(.*)$ $1 break;proxy_pass http://127.0.0.1:9090;# proxy_pass http://127.0.0.1:42002;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location ^~ /myGoApp {if ($request_method = 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173';  add_header Access-Control-Allow-Headers '*';  add_header Access-Control-Allow-Methods '*';  add_header Access-Control-Allow-Credentials 'true';  return 204;  }  if ($request_method != 'OPTIONS') {  add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  add_header Access-Control-Allow-Credentials 'true';  }access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";# 将 /myGoApp 替换为空,只保留后续的路径rewrite ^/myGoApp(.*)$ $1 break;proxy_pass http://127.0.0.1:8000;# proxy_pass http://127.0.0.1:42002;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

相关文章:

  • VMware虚拟机突然无法ssh连接
  • 消除爆红 [vue/no-unused-vars] ‘row‘ is defined but never used.eslint-plugin-vue
  • 【NGINX】-11 nginx 平滑升级
  • 如何使用patch-package给npm包打补丁
  • halcon 连接相机
  • 扫描电镜:打开微观世界的“超维相机“
  • 晶圆隐裂检测提高半导体行业效率
  • LNCS-2009《Adaptive Sampling for $k$-Means Clustering》
  • 探索Dify:开启大语言模型应用开发新时代
  • 2025电工杯数学建模A题思路数模AI提示词工程
  • 从细胞工厂到智能制造:Extracellular 用时序数据库 TDengine 打通数据生命线
  • LIEDNet: A Lightweight Network for Low-light Enhancement and Deblurring论文阅读
  • uni-app/vue2:微信小程序实现文件流的下载及预览
  • 【菜狗work前端】小程序加if判断时不及时刷新 vs Web
  • 微气象在线监测装置:精准感知环境变化的科技之眼
  • 微信小程序用<web-view 嵌入h5网页,改了h5网页后,可能是缓存的原因,小程序上看还是原来的,怎么处理
  • AI浪潮下,媒体内容运营的五重变奏
  • IP 地址反向解析(IP反查域名)的原理详解
  • 力扣HOT100之图论:200. 岛屿数量
  • 解决MybatisPlus使用Druid1.2.11连接池查询PG数据库报Merge sql error的一种办法
  • 网站变灰/推广普通话手抄报内容怎么写
  • 旅游商业网站策划书/360竞价推广客服电话
  • 网站关键词标签/seo优化服务公司
  • 网站升级改版需要多久/西安seo排名公司
  • 万网域名抢注/seo关键词排名公司
  • 专业网站设计建设/9个成功的市场营销案例