Nginx本身也是一个静态资源的服务器,当只有静态资源的时候,就可以使用Nginx来做服务器,如果一个网站只是静态页面的话,那么就可以通过这种方式来实现部署。
1、在文档根目录Docroot(/usr/local/var/www)下创建html目录, 然后在html中放一个test.html;
2、配置nginx.conf中的server:
user mengday staff;
http {
    server {
        listen       80;
        server_name  localhost;
        client_max_body_size 1024M;
        # 默认location
        location / {
            root   /usr/local/var/www/html;
            index  index.html index.htm;
        }
    }
}
=======>
注意:如果访问图片出现403 Forbidden错误,可能是因为nginx.conf 的第一行user配置不对,默认是#user nobody;是注释的,linux下改成user root; macos下改成user 用户名 所在组; 然后重新加载配置文件或者重启,再试一下就可以了, 用户名可以通过who am i 命令来查看。
4、指令简介
http://localhost/test.html,“/test.html”会匹配到”/”uri, 找到root为/usr/local/var/www/html,用户访问的资源物理地址=root + uri = /usr/local/var/www/html + /test.html=/usr/local/var/www/html/test.htmlserver_name时后面不跟任何路径是不走root直接走index指令的;如果访问路径中没有指定具体的文件,则返回index设置的资源,如果访问http://localhost/html/ 则默认返回index.html5、location uri正则表达式
. :匹配除换行符以外的任意字符? :重复0次或1次+ :重复1次或更多次* :重复0次或更多次\d :匹配数字^ :匹配字符串的开始$ :匹配字符串的结束{n} :重复n次{n,} :重复n次或更多次[c] :匹配单个字符c[a-z] :匹配a-z小写字母的任意一个(a|b|c) : 属线表示匹配任意一种情况,每种情况使用竖线分隔,一般使用小括号括括住,匹配符合a字符 或是b字符 或是c字符的字符串\ 反斜杠:用于转义特殊字符小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。
http {
    server {
        listen       80;
        server_name  localhost;
        set $doc_root /usr/local/var/www;
        # 默认location
        location / {
            root   /usr/local/var/www/html;
            index  index.html index.htm;
        }
        location ^~ /images/ {
            root $doc_root;
       }
       location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
           root $doc_root/img;
       }
    }
}
常见的location路径映射路径有以下几种:
=       进行普通字符精确匹配。也就是完全匹配。^~         前缀匹配。如果匹配成功,则不再匹配其他location。~      表示执行一个正则匹配,区分大小写~*         表示执行一个正则匹配,不区分大小写/xxx/   常规字符串路径匹配/       通用匹配,任何请求都会匹配到当一个路径匹配多个location时究竟哪个location能匹配到时有优先级顺序的,而优先级的顺序于location值的表达式类型有关,和在配置文件中的先后顺序无关。相同类型的表达式,字符串长的会优先匹配。
^~类型表达式,不属于正则表达式。一旦匹配成功,则不再查找其他匹配项,停止搜索。~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
upstream web_servers {  
   server localhost:8081;  
   server localhost:8082;  
}
server {
    listen       80;
    server_name  localhost;
    #access_log  logs/host.access.log  main;
    location / {
        proxy_pass http://web_servers;
        # 必须指定Header Host
        proxy_set_header Host $host:$server_port;
    }
 }
指定轮询几率,weight和访问比率成正比, 也就是服务器接收请求的比例就是各自配置的weight的比例,用于后端服务器性能不均的情况,比如服务器性能差点就少接收点请求,服务器性能好点就多处理点请求。
upstream test {
    server localhost:8081 weight=1;
    server localhost:8082 weight=3;
    server localhost:8083 weight=4 backup;
}
是4次请求只有一次被分配到8081上,其他3次分配到8082上。backup是指热备,只有当8081和8082都宕机的情况下才走8083
iphash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}
按后端服务器的响应时间来分配请求,响应时间短的优先分配。这个配置是为了更快的给用户响应。
upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backend {
    hash $request_uri;
    hash_method crc32;
    server localhost:8080;
    server localhost:8081;
}
动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作。
upstream web_servers {  
       server localhost:8081;  
       server localhost:8082;  
}
server {
    listen       80;
    server_name  localhost;
    set $doc_root /usr/local/var/www;
    location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
       root $doc_root/img;
    }
    location / {
        proxy_pass http://web_servers;
        # 必须指定Header Host
        proxy_set_header Host $host:$server_port;
    }
    error_page 500 502 503 504  /50x.html;  
    location = /50x.html {  
        root $doc_root;
    }
 }
原文:https://www.cnblogs.com/KL2016/p/14956958.html