【问题标题】:Remove Headers Added By WebServer (nginx) In Aspcore Application删除由 Web 服务器 (nginx) 在 Asp Core 应用程序中添加的标头
【发布时间】:2021-02-13 07:00:34
【问题描述】:

我使用的是aspcore sdk3.1,当我在我的应用程序中没有任何配置的情况下在nginx上发布我的应用程序时,我看到Web服务器自动为我的所有请求添加了1access-control-allow-origin:*1 Header。

我可以在我的应用程序中删除此标头并添加我自己的 Allow-Origin 标头吗? ,因为我无权访问网络服务器设置

【问题讨论】:

    标签: c# asp.net asp.net-core nginx webserver


    【解决方案1】:

    如果nginx添加了CORS allow all origin header,那么肯定有this one here这样的配置

    #
    # Wide-open CORS config for nginx
    #
    location / {
         if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            #
            # Om nom nom cookies
            #
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
         if ($request_method = 'GET') {
            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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
    }
    
    • 只需将add_header 行删除到您不需要它们的任何位置,就可以了。
    • 请注意,您不要更改所有应用程序,其他人可能正在使用它,因此请仅针对您的位置进行配置。
    • 如果您没有访问权限,只需与管理员联系,让您为应用程序进行新配置。

    似乎https://serverfault.com/questions/751678/how-can-i-replace-access-control-allow-origin-header-in-proxy-response-with-ngin/927668#927668 add header 会覆盖net-core配置添加到请求中的那个,所以你似乎不走运。

    【讨论】:

      【解决方案2】:

      http {

      ##
      # Basic Settings
      ##
      
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      keepalive_timeout 65;
      types_hash_max_size 2048;
      # server_tokens off;
      
      # server_names_hash_bucket_size 64;
      # server_name_in_redirect off;
      
      include /etc/nginx/mime.types;
      default_type application/octet-stream;
      
      ##
      # SSL Settings
      ##
      
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
      ssl_prefer_server_ciphers on;
      
      ##
      # Logging Settings
      ##
      
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
      
      ##
      # Gzip Settings
      ##
      
      gzip on;
      
      #proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m
      #inactive=14d  max_size=10g;
      
      upstream dotnet {
          zone dotnet 64k;
          server 0.0.0.0:5005;
      } 
      
      # gzip_vary on;
      # gzip_proxied any;
      # gzip_comp_level 6;
      # gzip_buffers 16 8k;
      # gzip_http_version 1.1;
      # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
      
      ##
      # Virtual Host Configs
      ##
          include /etc/nginx/conf.d/*.conf;
      include /etc/nginx/sites-enabled/*;
      

      }

      这是我的配置代码有什么问题?

      【讨论】:

        猜你喜欢
        • 2019-02-26
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-05
        • 1970-01-01
        相关资源
        最近更新 更多