nginx禁止跨域,防范cors攻击

参考了这篇文章:nginx禁止跨域_杰克琼斯D雨的博客-CSDN博客

map $http_origin $cors_origin {
    default 1;
    "~^(http?://(localhost)(:[0-9]+)?)$" 1;
    "~^(http?://(192.168.xx.xx)(:[0-9]+)?)$" 1;
    "~*" 0;
}


server {
    listen 81;
    access_log  /var/log/backend.log ;
    server_name _;
        
    location / {
        include uwsgi_params;
        proxy_pass  http://127.0.0.1:8000/api/;
        error_page  404 /;
    }
}

server {
    listen 8080;
    server_name _;
    access_log  /var/log/frontend.log ;

    location /notification/ {
        proxy_pass http://127.0.0.1:8000/notification/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }


    location ^~ /bak/ {
        alias /bak/;
 
        if ($request_filename ~* ^.*?\.(des3|doc|pdf|tar\.gz|zip|docx|txt)$) {
            add_header Content-Disposition attachment;
            add_header Content-Type application/octet-stream;
        }
        sendfile on;   
        autoindex on;  
        autoindex_exact_size on;  
        autoindex_localtime on;  
        charset utf-8,gbk;  
    }


    location ^~/api/ {
        proxy_pass http://127.0.0.1:81/;
        proxy_redirect  off;
        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-NginX-Proxy true;
        error_log /var/log/error_api.log;

        if ($cors_origin = 0) {
            return 403;
        }

    }

    location / {
        try_files $uri $uri/ /index.html;
        root /www/dist;
        index index.html index.htm;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为了防止CORS漏洞,可以在Nginx服务器上进行以下设置: 1. 在Nginx配置文件中添加以下内容: ```nginx location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; if ($request_method = 'OPTIONS') { return 204; } } ``` 2. 在Nginx配置文件中添加以下内容,以允许特定的域名进行跨域请求: ```nginx location / { if ($http_origin ~* (http://localhost:8080|http://www.example.com)) { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; } if ($request_method = 'OPTIONS') { return 204; } } ``` 3. 在Nginx配置文件中添加以下内容,以允许所有域名进行跨域请求: ```nginx location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; if ($request_method = 'OPTIONS') { return 204; } } ``` 4. 在Nginx配置文件中添加以下内容,以允许特定的域名进行跨域请求,并且允许特定的HTTP方法: ```nginx location / { if ($http_origin ~* (http://localhost:8080|http://www.example.com)) { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; } if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; return 204; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扬子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值