Update:There is now a follow up to this post which deals with thedifferences between Nginx and Apache, it is recommended reading if you come from Apache:
Nginx is a fairly simple HTTP server, though there are a few gotchas people need to be aware of before they start using this 8th wonder. The most important is that Nginx is a reverse proxy first and HTTP server second, it does not necessarily have a concept of file, this will change the way we handle our configuration a bit.
The first thing you need to know is that the Nginx configuration file is an inheriting-hierarchy, directives specified in a higher block will filter down to lower blocks as a default value, from this follows that we want to specify things in the top most hierarchy whenever possible. Since directives in top blocks filter down as default values it is still possible to override them in most cases.
There are 3 hierarchies which are usually referred to as blocks. The HTTP-block, the server-block and the location block of which the hierarchy goes like this: http -> server -> location.
Furthermore there are two special locations, an event block and the root which the event block and the http block reside in. Both of these contain only a minor amount of directives. The majority of your time will be spent in the other three blocks.
The blocks have a semantic meaning of sorts. The server block is what in Apache would be considered a virtual host. The location block usually referrers to the URI.
When using the official wiki the context keyword specifies in which block a directive may be used, as mentioned earlier it is usually recommended to specify the directive in the top most block.
Virtual Hosts
To begin with the most interesting directives are server_name and root. The former instruct Nginx to use this server block when the HOST header matches the value and the latter defines what to use as root when looking for files.
This forms the basic of our virtual hosts, an example would be:
server { listen 80; server_name domain.com *.domain.com; rewrite ^ http://www.domain.com$request_uri? permanent; } server { listen 80; server_name www.domain.com; index index.html; root /home/domain.com }
Here we have two virtual hosts. The first one is hit when domain.com or any subdomain of domain.com except for www is sent as the HOST header by the browser. The reason for this is that Nginx will always choose the most specific match, and if visting www.domain.com then this will match the second block precisely.
This also means you can create a default virtual host to catch all domains without a proper match. Thankfully this is as simple as adding the default_server flag to the listen directive. This causes all request without a HOST header or without another vhost matching the HOST header to be sent to this vhost instead. Examples are requests accessing the IP directly or if someone points a random domain at your IP. The server_name _; which you will see mentioned in a lot of guides means nothing and does nothing. It’s just a bogus invalid HOST header that can be used to make sure nothing ever matches it. You should be able to simply not define a server_name.
server { listen 80 default_server; server_name _; index index.html; root /var/www/default }
Locations
If you’re changing over from Apache then this is where you want to pay attention. Nginx typically does not use complicated rewrites – usually we can accomplish the same using a location block.
The most important things to note are that locations, with the exception of named locations, works on the URI without any query parameters and only one location block will ever be run. This is also why I recommend putting directives in the top most block. A root directive defined in location / will not be available in location /images – unless defined in the server block. I trust you see how defining things in the upper most block will prevent code duplication and headaches.
Another important point about locations is that, as with server_name directive, Nginx will use the most specific location block. There are a few rules for how specific various setups will be, thelocation directive wiki entryexplains this very well, so you should read that first.
Let us look at a few examples of how to use a location block. In this example we’ve run a forum on URI /forum/ and have recently moved it to a subdomain. We now need to redirect the old URLs to the new URLs.
server { listen 80 default; server_name www.domain.com; root /home/domain.com # This will match any URI beginning with /forum location /forum { # We capture the URI and redirect it to the subdomain. rewrite forum(.*) http://forum.domain.com$1 permanent; } } server { listen 80; server_name forum.domain.com; index index.php; root /home/domain.com/forum; }
The requests for /forum are now successfully transferred to our new subdomain while requests to files not in /forum will be served from our normal /home/domain.com root.
Handling PHP
PHP – or any backend really ties in well with locations, namely we can define a location block to catch all PHP files.
server { listen 80; server_name forum.domain.com; index index.php; root /home/domain.com/forum; location ~* \.php$ { include fastcgi.conf # I include this in http context, it's just here to show it's required for fastcgi! try_files $uri =404; fastcgi_pass 127.0.0.1:9000; } }
As said previously, Nginx does not care about files but rather locations and this is why I have a try_files directive inside the php block. This location block matches a URI that ends in .php but it does not care if it’s a file or not. Therefore a request for /forum/avatars/user2.jpg/index.php will be matched and sent to PHP, and if PHP is not configured properly PHP will then execute /forum/avatars/user2.jpg when /forum/avatars/user2.jpg/index.php doesn’t exist. This provides a huge security risk. Do note that this is not a bug in Nginx, it’s the intended behaviour and as such will not be “fixed”.
This can also be fixed on the PHP side by setting cgi.fix_pathinfo=0 in the php.ini file.
The end result, though, is that .php files which exist will be passed via fastcgi to our PHP processes running on port 9000.
The Finishing Touch – SEF URLs
This setup works, but all the craze these days is to have search engine friendly URLs for SEO reasons. Usually this involves quite a few rewrites, but with Nginx we can do it with just one line, provided the backend script is written in a sane way.
server { listen 80; server_name forum.domain.com; index index.php; root /home/domain.com/forum; location / { try_files $uri $uri/ /index.php; } location ~* \.php$ { include fastcgi.conf # I include this in http context, it's just here to show it's required for fastcgi! try_files $uri =404; fastcgi_pass 127.0.0.1:9000; } }
Did you notice the change? It’s minimal really. The one try files line means that it will first try accessing the full URI, which means that a static file request will end here. Secondly it will try the full URI plus a slash, thus looking for a directory. Finally, if none of these are found it will send the request to /index.php and perform a new location match, which will of course hit our PHP location and fastcgi_pass the request. PHP will then have the full URI in $_SERVER['PATH_INFO']. Simple, elegant and easy to understand.
Debugging Requests
Nginx is a complicated server at times, thankfully we have an excellent error log available to us to help figure out where things are going wrong. If you check theerror log directive in the wikiyou will notice that it takes a second argument. This will let you define how much information is output by nginx. A value of info will give you sufficient info to debug most issues.
Further Reading
Theofficial nginx wikiis an invaluable resource, a good way to expand your knowledge about Nginx is to read the directives and variables in theCore module. Also see thefull config exampleto get an idea of a more complex configuration file.
No related posts.
相关推荐
arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构...
现在,我们可以下载Nginx的源代码包`nginx-1.20.1.tar.gz`。你可以通过wget或者浏览器将文件下载到本地,然后解压: ```bash wget http://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1.tar.gz cd...
**Nginx版本升级步骤详解** 在Web服务器领域,Nginx以其高性能、低内存消耗以及高并发处理能力而备受青睐。随着新版本的发布,可能会包含性能优化、安全修复和新特性,因此定期更新Nginx版本是必要的。本文将详细...
这个名为"nginx-linux-arm64.zip"的压缩包提供的是专为ARM64架构(也称为AArch64)编译的Nginx版本,适用于基于Linux操作系统的64位ARM处理器设备,如树莓派、某些云服务器或嵌入式系统。无需繁琐的编译过程,只需...
**Nginx 1.13.3 版本详解** Nginx 是一款高性能的 HTTP 和反向代理服务器,广泛应用于网站托管、负载均衡以及应用程序交付等领域。它以其高效、稳定和轻量级的特性著称,尤其在处理静态内容和高并发请求时表现优秀...
**Nginx与Nginx-RTMP及Nginx-HTTP-FLV模块** Nginx是一款高性能、轻量级的Web服务器/反向代理服务器,被广泛应用于高并发场景,尤其在处理静态文件、HTTP缓存以及反向代理等方面表现出色。Nginx以其高效的事件驱动...
### Nginx 作为 Apache 和 JBoss 的替代方案 #### 背景介绍 随着互联网技术的不断发展,网站流量的增长对服务器性能提出了更高要求。Apache 和 JBoss 是两种广泛使用的 Web 服务器和应用服务器,但在高并发场景下,...
实战nginx.pdf。主要内容包括:第1章 Nginx简介;第2章Nginx服务器安装与配置;第3章Nginx基本配置与优化;第4章Nginx与PHP;第5章Nginx与JSP、ASP.NET..第6章Nginx http负载均衡和反向代理;第7章Nginx 的rewrite...
Nginx 1.24.0 是 Nginx 开源项目发布的一个重要更新版本,该版本在性能优化、功能增强以及安全性提升方面带来了诸多改进。当您下载 Nginx 1.24.0 的压缩包时,您将获得一个包含 Nginx 源代码的压缩文件,通常命名为 ...
在 Linux 系统上升级 Nginx 版本 Nginx 是一个流行的开源 Web 服务器软件,可以运行在多种操作系统上,其中包括 Linux。随着 Nginx 的不断更新和发展,升级 Nginx 版本成为一个不可避免的问题。本文将指导您在 ...
Nginx课件完整版.pdf Nginx是一款功能强大的网络服务器软件,能够提供高性能的Web服务器、反向代理、负载均衡等功能。本资源摘要信息将对Nginx的主要知识点进行详细的介绍。 什么是Nginx? Nginx是一个基于C语言...
在Linux系统中,离线安装Nginx是一个常见的需求,特别是在没有互联网连接或者网络环境受限的服务器上。本文将详细讲解如何通过离线方式在Linux上安装Nginx,同时也会涉及Nginx依赖的软件如openssl和gcc的安装过程。 ...
在IT领域,尤其是在服务器配置和优化的过程中,ARM架构和Nginx扮演着至关重要的角色。ARM(Advanced RISC Machines)架构是一种广泛应用于嵌入式设备、移动设备以及高性能计算的处理器架构,以其低功耗和高效能而...
在IT行业中,Nginx是一款广泛应用的高性能Web服务器和反向代理服务器,它以其轻量级、高并发处理能力和稳定性而著称。本资源提供的是一款针对Windows平台的Nginx,其中已经集成了`nginx-http-flv-module`模块,这个...
在Ubuntu 18.04系统中安装Nginx服务器是一项常见的任务,特别是在无互联网连接的环境下,离线安装显得尤为重要。本资源提供了一个适用于这种场景的解决方案,它包括了Ubuntu 18.04环境下Nginx的离线安装包。这个离线...
**Nginx 本地文件映射详解** 在IT行业中,Nginx是一个广泛使用的高性能Web服务器和反向代理服务器,以其高效、稳定和轻量级的特性受到青睐。其中一个实用的功能是通过配置来映射本地文件,使得用户可以通过HTTP协议...
Nginx 1.8.0 安装简述 Nginx 是一种流行的开源 Web 服务器软件,广泛应用于生产环境中。为了帮助读者快速掌握 Nginx 的安装过程,本文将详细介绍 Nginx 1.8.0 的安装步骤。 一、下载依赖项 在安装 Nginx 之前,...
"GitLab系统中Nginx版本升级和配置" 在实际生产环境中,GitLab系统的Nginx版本升级和配置是一个非常重要的任务。为确保系统的稳定性和安全性,需要对GitLab系统中的Nginx版本进行升级和配置。本文将详细介绍如何...
1. **下载源码**:首先,从Nginx官网获取稳定版本的源代码,例如nginx-1.17.10。同时,从GitHub或其他可靠的来源下载HTTP FLV Module的源代码。 2. **安装编译工具**:Windows上需要安装MinGW或Visual Studio等编译...
Nginx 1.22.0 Linux 版本,解压安装。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型...