Daily

常用 Nginx配置

参考网站

https://www.nginx.com/resources/wiki/start/topics/examples/full/

Default.cnf

user       www www;  ## Default: nobody
worker_processes  5;  ## Default: 1
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  include    conf/mime.types;
  include    /etc/nginx/proxy.conf;
  include    /etc/nginx/fastcgi.conf;
  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log   logs/access.log  main;
  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128; # this seems to be required for some vhosts
  include conf.d/*.conf;
}

网站配置1

server {
    listen          8080;
    server_name     big.server.com;
    access_log      logs/big.server.access.log main;

    location / {
        root   /etc/nginx/upload;
        index  index.html index.htm;
    }

    # Upload form should be submitted to this location
    location /upload {

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        upload_store_access user:r;

        # Set specified fields in request body
        upload_set_form_field $upload_field_name.name "$upload_file_name";
        upload_set_form_field $upload_field_name.content_type "$upload_content_type";
        upload_set_form_field $upload_field_name.path "$upload_tmp_path";

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
        upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";

        upload_pass_form_field "^submit$|^description$";

        upload_cleanup 400 404 499 500-505;
    }
  }

网站配置2

server {
    server_name hitdic.com;

    index index.html;
    root  /path/to/dist;

    # 指定静态文件目录
    location / {
        alias /path/tp/dist/;
        #index index.html;
        try_files $uri $uri/ /index.html;
    }

    # 重定向到api
    location /image/ {
        proxy_pass https://image.hitdic.com/;
        proxy_set_header Host image.hitdic.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    重定向到指定文件
    location /statistics {
        return 301 /docs/download/downloadlist.html;
    }

    # 重定向到api
    location /api3rd/ {
        proxy_pass https://api.hitdic.com/api/v1/;
        proxy_set_header Host api.hitdic.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # 重定向到阿里云的OSS
    location /versions/ {
        proxy_pass http://buketname.oss-cn-hongkong-internal.aliyuncs.com/path/to/;
    }
}