欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

常见配置

禁用cors

location / {
  location ~* ^.+\.(?:css|cur|json|js|jpe?g|gif|htc|ico|png|txt|otf|ttf|eot|woff|svg|webp|webm|zip|gz|tar|rar)$ {
    set $test_http_origin a;
    if ($http_origin != "") {
      set $test_http_origin x;
    }
    if ($http_origin != "${scheme}://${host}") {
      set $test_http_origin "${test_http_origin}x";
    }
    if ($test_http_origin = xx) {
      add_header "Access-Control-Allow-Origin" "*";
    }
    access_log off;
    expires 30d;
    ## No need to bleed constant updates. Send the all shebang in one
    ## fell swoop.
    tcp_nodelay off;
    ## Set the OS file cache.
    open_file_cache max=3000 inactive=120s;
    open_file_cache_valid 45s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
    try_files $uri $uri/ @cache;
  }
}

最简单的nginx服务器

server {     // 虚拟主机server可以有多个      
  listen      *:80 default_server;
  server_name netguru.co;
  return 200 "Hello from netguru.co";
}

nginx http到https转发

参考redirect http to https.

server {
    listen 80 default_server;

    server_name _;

    return 301 https://$host$request_uri;
}

nginx静态文件服务器

server {
    listen 80 default_server;
    listen [::]:443 ssl default_server;

    root /var/www/your_project_root
    server_name your_domain.com

    location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
}