ini Nginx,PHP和CORS。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ini Nginx,PHP和CORS。相关的知识,希望对你有一定的参考价值。

server {
    listen 80;

    server_name mydomain.com www.mydomain.com;

    location / {
        rewrite ^(.*)$ https://$server_name$1 permanent;
    }
}

server {
    listen 443 ssl;

    server_name mydomain.com;

    ssl_certificate      /path_to_certificate/fullchain.cer;
    ssl_certificate_key  /path_to_certificate/mydomain.com.key;

    root /var/www;

    index index.html index.htm index.php;

    charset utf-8;

    location / {

        # ------------------------------------------------
        # CORS - http://enable-cors.org/server_nginx.html
        # ------------------------------------------------

        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*'; # Change * to mydomain.com in production
            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' '*'; # Change * to mydomain.com in production
            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' '*'; # Change * to mydomain.com in production
            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';
        }

        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ /\.git {
        deny all;
    }
}

以上是关于ini Nginx,PHP和CORS。的主要内容,如果未能解决你的问题,请参考以下文章

ini 用于nginx的全开CORS配置

ini 用于nginx的全开CORS配置

ini 用于nginx的全开CORS配置

ini CORS-nginx.conf

ini 用nginx添加CORS头

ini CORS-nginx.conf