1、配置监听 80 端口

   server {
        listen       80;
        server_name  blog.whitepiao.cf;
        location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header   Host    $host;
            proxy_set_header   X-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       }


#图片站点
    server {
        listen       80;
        server_name  your domain name;
        location / {
            root  图片路径;
            autoindex on;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       }

#静态站点配置
      server {
        listen       80;
        server_name  blog.whitepiao.cf;
        location / {
            root  网站路径;
            index  index.html;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       }

2、配置监听 443 端口

1、特别需要注意文件夹中首字符带有 n 的路径需要双\ \转义, 否则\n 会被识别为换行导致无法识别该路径

2、申请的 SSL 证书必须是 nginx 版,把证书放至 nginx\conf\ssl 文件夹下

server {
      listen       443 ssl;
      server_name  blog.whitepiao.cf;

      ssl_certificate      \ssl\blog.whitepiao.cf\blog.whitepiao.cf_bundle.crt;
      ssl_certificate_key  \ssl\blog.whitepiao.cf\blog.whitepiao.cf.key;

      #ssl_session_cache    shared:SSL:1m;
      ssl_session_timeout  5m;

      ssl_ciphers  HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers  on;

location / {
	proxy_pass http://127.0.0.1:8000;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
  }


3、解决上传最大文件限制

nginx 默认的上传文件大小是有限制的,一般为 2MB,如果你要上传的文件超出了这个值,将可能上传失败。

client_max_body_size 500m; 为上传最大文件限制,可加载 http、server、location 分别对应不同的作用域

server {
    listen       80;
    server_name  blog.whitepiao.cf;
    client_max_body_size 500m; # 最大上传500m
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 50m;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
   }


4、手机端访问 PC 端网站强制跳转到手机端

  server {
      listen       80;
      server_name  blog.whitepiao.cf;
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
        rewrite  ^(.*)    手机端地址$1 permanent;
}
      location / {
	proxy_pass http://127.0.0.1:8000;
	proxy_set_header   Host    $host;
	proxy_set_header   X-Real-IP   $remote_addr;
	proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      }
      #error_page  404              /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
     }


5、HTTP 强制跳转到 HTTPS

#重定向方法
server
{
    listen 80;
    server_name your domain name;
    rewrite ^/(.*)$ http://your domain name/$1 permanent;
}
或者
server
{
    listen 80;
    server_name your domain name;
    rewrite ^ http://your domain name$request_uri? permanent;
}
#使用301返回
server
{
    listen 80;
    server_name your domain name;
    return 301 https://$server_name$request_uri;
}


6、Nginx 静态资源缓存设置

参考链接:https://www.w3cschool.cn/nginxsysc/nginxsysc-cache.html

对于站点中不经常修改的静态内容(如图片,JS,CSS),可以在服务器中设置 expires 过期时间,控制浏览器缓存,达到有效减小带宽流量,降低服务器压力的目的。

以 Nginx 服务器为例:

 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    #过期时间为30天,
    #图片文件不怎么更新,过期可以设大一点,
    #如果频繁更新,则可以设置得小一点。
    expires 30d;
}

location ~ .*\.(js|css)$ {
	expires 10d;
}

#只缓存image文件夹下的文件
 location /image {
    expires 30d;
}

7、一种域名多种用途

此情况适用以下业务场景:

一个域名可以带参数跳转到另一个网站

server {
        listen       80;
        server_name  blog.whitepiao.cf;

        location / {
        	#带参数跳转
			if ($args ~* "Id"){
			rewrite ^/(.*)$ http://bushu.whitepiao.cf/$1 permanent;
			}
			proxy_pass http://127.0.0.1:8000;
			proxy_set_header   Host    $host;
			proxy_set_header   X-Real-IP   $remote_addr;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       }