Nginx Learning (4)

Configuring NGINX Plus as a Web Server

At a high level, configuring NGINX Plus as a web server is a matter of defining which URLs it handles and how it processes HTTP requests for resources at those URLs. At a lower level, the configuration defines a set of virtual servers that control the processing of requests for particular domains or IP addresses.

Setting Up Virtual Servers

If a port is omitted, the standard port is used. Likewise, if an address is omitted, the server listens on all addresses.

If there are several servers that match the IP address and port of the request, NGINX Plus tests the request’s Host header field against the server_name directives in the server blocks.

The parameter to server_name can be a full (exact) name, a wildcard, or a regular expression. A wildcard is a character string that includes the asterisk (*) at its beginning, end, or both; the asterisk matches any sequence of characters.

If the Host header field does not match a server name, NGINX Plus routes the request to the default server for the port on which the request arrived. The default server is the first one listed in the nginx.conf file, unless you include the default_server parameter to the listen directive to explicitly designate a server as the default.

Configuring Locations

There are two types of parameter to the location directive: prefix strings (pathnames) and regular expressions. For a request URI to match a prefix string, it must start with the prefix string.

A regular expression is preceded with the tilde (~) for case-sensitive matching, or the tilde-asterisk (~*) for case-insensitive matching.

To find the location that best matches a URI, NGINX Plus first compares the URI to the locations with a prefix string. It then searches the locations with a regular expression.

TODO

server {
    location /images/ {
        root /data;
    }

    location / {
        proxy_pass http://www.example.com;
    }
}

The root directive specifies the file system path in which to search for the static files to serve. The request URI associated with the location is appended to the path to obtain the full name of the static file to serve. In the example above, in response to a request for /images/example.png, NGINX Plus delivers the file /data/images/example.png.

Returning Specific Status Codes

The first parameter of return is a response code. The optional second parameter can be the URL of a redirect (for codes 301302303, and 307) or the text to return in the response body.

location /permanently/moved/url {
    return 301 http://www.example.com/moved/here;
}

Rewriting URIs in Requests

A request URI can be modified multiple times during request processing through the use of the rewrite directive, which has one optional and two required parameters. The first (required) parameter is the regular expression that the request URI must match. The second parameter is the URI to substitute for the matching URI. The optional third parameter is a flag that can halt processing of further rewrite directives or send a redirect (code 301 or 302).

关于last和break

(ref: https://serverfault.com/questions/131474/nginx-url-rewriting-difference-between-break-and-last)

You may have different sets of rewrite rules for different locations. When rewrite module meets last, it stops processing the current set and the rewritten request is passed once again to find the appropriate location (and the new set of rewriting rules). If the rule ends with break, the rewriting also stops, but the rewritten request is not passed to another location.

That is, if there are two locations: loc1 and loc2, and there‘s a rewriting rule in loc1 that changes loc1 to loc2 AND ends with last, the request will be rewritten and passed to location loc2. If the rule ends with break, it will belong to location loc1.

Rewriting HTTP Responses

Sometimes you need to rewrite or change the content in an HTTP response, substituting one string for another. You can use the sub_filter directive to define the rewrite to apply.

Another example changes the method from http:// to http_s_:// and replaces the localhost address to the host name from the request header field. The sub_filter_once directive tells NGINX to apply sub_filter directives consecutively within a location:

location / {
    sub_filter     ‘href="http://127.0.0.1:8080/‘    ‘href="https://$host/‘;
    sub_filter     ‘img src="http://127.0.0.1:8080/‘ ‘img src="https://$host/‘;
    sub_filter_once on;
}

Note that the part of the response already modified with the sub_filter will not be replaced again if another sub_filter match occurs.

Handling Errors

In the following example, when NGINX Plus cannot find a page, it substitutes code 301 for code 404, and redirects the client to http:/example.com/new/path.html. This configuration is useful when clients are still trying to access a page at its old URI.

location /old/path.html {
    error_page 404 =301 http:/example.com/new/path.html;
}

原文地址:https://www.cnblogs.com/geeklove01/p/9195883.html

时间: 2024-08-06 23:01:18

Nginx Learning (4)的相关文章

Nginx Learning (1)

前言: 作为战斗民族的发明,Nginx在服务器网络处理方面因其稳定,高效,简洁的配置,出色的表现而被广泛使用 Docs written in English are the best material of mastering a specific technology. (ref: https://docs.nginx.com/nginx/) Controlling NGINX Processes at Runtime Master and Worker Processes NGINX has

Nginx Learning (3)

NGINX Load Balancing – TCP and UDP Load Balancer Configuring Reverse Proxy For UDP traffic, also include the udp parameter. TCP is the default protocol for the stream context, so there is no tcp parameter to the listen directive Optionally, you can t

Nginx Installation、Configuration、Rreverse Proxy、Load Balancing Learning

目录 1. Nginx简介 2. Nginx安装部署 3. Nginx安全配置 4. Nginx反向代理实践 5. Nginx负载均衡实践 1. Nginx简介 0x1: Nginx的基本特性 Nginx("engine x")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器Nginx可以在大多数Unix like OS上编译运行,并有Windows移植版.它的的源代码使用2-clause BSD-like licenseNginx是一个很强大的高

Nginx +WAF使用总结

曾经研究过一段时间,并做了一下简单的总结,感觉还比较实用,放在有道云笔记里躺了很长时间,感觉有点浪费,拿出来分享一下,让新手少走弯路,高手请绕行.... 环境: linux平台,redhat系统: 一 Nginx 安装 1.所需组件 若已有这几个组件,可直接查看安装部分 1.1  gcc编译器 若没有gcc编译器, yum -y install gcc* 这一步时间有点长 1.2  pcre-8.32 安装pcre过程: http://sourceforge.net/projects/pcre/

Nginx安全加固配置手册

第1章   概述 1.1   目标 Nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,可以稳定地运行在Linux.Windows等操作系统上,其特点是占用内存少,并发能力强. 同其他软件一样,Nginx也出现过一些安全漏洞,利用这些漏洞可以对Web服务器进行渗透攻击.本文主要描述互联网架构中常用产品Nginx 的配置和安全加固工作,最终用以指导系统实施. 1.2   预期读者

nginx上传模块—nginx upload module-

一. nginx upload module原理 官方文档: http://www.grid.net.ru/nginx/upload.en.html Nginx upload module通过nginx服务来接受用户上传的文件,自动解析请求体中存储的所有文件上传到upload_store指定的目录下.这些文件信息从原始请求体中分离并根据nginx.conf中的配置重新组装好上传参数,交由upload_pass指定的段处理,从而允许处理任意上传文件.每个上传文件中的file字段值被一系列的uplo

nginx反向代理服务器域名解析配置实操

1.进入nginx目录cd conf:创建文件夹 sudo mkdir vhost,cd vhost,sudo vim www.fanxing.store.conf,添加如下内容 server { default_type 'text/html'; charset utf-8; listen 80; autoindex on; server_name www.fanxing.store; access_log /usr/local/nginx/logs/access.log combined;

6、nginx配置

linux 下nginx的安装 安装依赖的库文件 yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel 下载nginx的安装包 wget http://learning.happymmall.com/nginx/linux-nginx-1.10.2.tar.gz 解压并且进入nginx的主目录 tar -zxvf nginx-xxx

Nginx使用Naxsi搭建Web应用防火墙(WAF),防xss、防注入×××

Naxsi是一个开放源代码.高效.低维护规则的Nginx web应用防火墙(Web Application Firewall)模块.Naxsi的主要目标是加固web应用程序,以抵御SQL注入.跨站脚本.跨域伪造请求.本地和远程文件包含漏洞.官网地址:https://github.com/nbs-system/naxsi Naxsi 不要求任何特定的依赖,它需要的 libpcre ,libssl ,zlib ,gzip 这些 Nginx 已经集成了. 下载Naxsi模块 [[email prote