美文网首页
Nginx--配置Web服务器

Nginx--配置Web服务器

作者: caiger | 来源:发表于2018-04-20 10:28 被阅读0次

    用Nginx作为web服务器,是在配置文件中配置一系列针对特殊域名或者ip地址的虚拟服务器(virtual server)配置,接收需要处理的URL以及请求这些URL对应资源的HTTP请求

    http.conf

    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        sendfile        on;
        keepalive_timeout  65;
        gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            location ~ \.php$ {
                proxy_pass   http://127.0.0.1;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            
            location ~ /\.ht {
                deny  all;
            }
        }
    
        server {
            listen       8000;
            listen       somename:8080;
            server_name  somename  alias  another.alias;
    
            error_page 404 /404.html;
            location / {
                root   html;
                index  index.html index.htm;
            }
            
            location ~ \.txt) {
                return 404 ;
            }
            location /some/path/ {
                proxy_set_header Host $host;
                proxy_pass http://www.example.com/link/;
            }
        }
    }
    

    设置虚拟主机(ngx_http_core_module)

    一个http上下文中可以配置多个server
    listen指令配置监听的地址和端口,未指定地址是监听所有地址,未指定端口是监听默认端口80或8000。
    server_name指令设置的域名,对应请求中header中的Host。指令的参数按照匹配顺序依次为:

    • 精确匹配
    • 以通配符“*”开头的匹配
    • 以“*”结尾的匹配
    • 以 “~” 开头的正则表达式匹配(顺序第一个)

    如果没有找到匹配的域名,则使用第一个server作为默认server,或者用default_server参数在listen指令中指定。

    配置location匹配URI(ngx_http_core_module)

    location 指令用来匹配服务器接收到的不同URI,经过指令集的处理后转发到对应的代理服务器、返回给相应返回码、本地资源文件或者重定向到其他URI。
    URI匹配的方式根据优先级顺序依次为:

    • 精确匹配: location = uri,配置“location = /” 可以提高访问速度
    • 最长终止前缀匹配: location ^~ prefix
    • 最先正则匹配: location ~ regex (大小写敏感),location ~* regex (大小写不敏感)
    • 最长前缀匹配:location prefix

    对URI进行重写(ngx_http_rewrite_module)

    在location块中,可以对匹配到的uri进行处理:
    rewrite regex replacement [flag]
    对正则匹配到的请求uri进行替换,可以位于server和location上下文中。整个过程在ngx_http_rewrite_module中进行:

    1. 先依次顺序执行一次server中的rewrite,若命中则根据替换后的新uri匹配对应的location
    2. 然后依次处理匹配到的location中的rewrite,若uri被重写,在处理完所有rewrite后,会对替换后的新uri重新匹配location
    3. 重复过程2,最多10次。

    如果replacement中包含新的参数,那么请求中原有的参数会跟在新参数后边,如果不想要原参数,可以在新参数后加“?”

    flag用于对rewrite进行控制:

    • last 停止当前的rewrite过程,并对替换后的新uri匹配新的location,并执行新的location中的rewrite过程(如果有)。
    • break 停止当前rewrite过程,且不对替换后的新uri匹配新的location,直接执行当前location的后续指令
    • redirect 返回 temporary redirect 302,如果replacement以“http://”、“https://”或“$scheme”开头,也直接返回302
    • permanent 返回301

    return code [text]/code URL/URL
    返回信息给对应的请求,通常为http状态码、状态码 返回信息、重定向码 URL。

    静态文件和错误页(ngx_http_core_module)

    root path
    指定本地文件搜寻的根目录,可以用于http、server、location上下文中,查找时作为匹配到的uri的前缀,从本地文件系统中获取资源,相对路径的根目录可以在nginx编译安装时通过参数指定,默认为../nginx/。
    alias path
    用于替换匹配到的location。如果是前缀匹配,则替换前缀;如果是正则,则必须包含捕获部分,并在替换path引用捕获部分。
    index file
    用来标识index文件名。当一个uri是以“/”结尾的时候,nginx认为它是在请求一个文件目录,会返回由index指令指定的文件,如果不存在则返回404。这个过程是一个内部重定向的过程,重定向的uri以匹配到的uri作为前缀,index文件名作为后缀,重新执行location匹配的过程,并返回最终结果。

    error_page code ... [=[response]] uri
    将指定的状态码转换为response、将请求uri内部重定向(internal redirect)为指定uri重新匹配location、将请求方式改为GET
    try_files file ... uri/=code;
    根据顺序检查指定文件是否存在,路径基于root或alias指令,

    反向代理(ngx_http_proxy_module)

    proxy_pass URL
    将对应location的请求代理成指定的协议和代理服务器。协议可以是http或https,地址可以时域名或者ip地址,如果是一组ip地址,可以使用负载均衡策略。URL中包含URI时,可以对前缀匹配的location进行替换,此时请求URI不能被重写,替换URI不能包含变量。
    proxy_set_header field value
    对传递给代理服务器的请求中的参数进行设置,可以在location、server、http上下文中设置。

    相关文章

      网友评论

          本文标题:Nginx--配置Web服务器

          本文链接:https://www.haomeiwen.com/subject/ombihftx.html