Skip to content

Instantly share code, notes, and snippets.

@zhiephie
Forked from mul14/nginx.conf
Created September 17, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiephie/2db048746d20fadeb0e3bd54be79487d to your computer and use it in GitHub Desktop.
Save zhiephie/2db048746d20fadeb0e3bd54be79487d to your computer and use it in GitHub Desktop.
Nginx, PHP and 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment