nginx比apache httpd用起来更灵活,本文介绍centos上的nginx安装与配置过程。
参考资料
官网上的资料很详细:http://nginx.org/
入门介绍:http://nginx.org/en/docs/beginners_guide.html
安装
主要命令
执行如下几条命令即可:
- 检查机器上是否安装过:rpm -qa nginx
- 查询是否有源可用:yum -y list nginx
- 安装:yum -y install nginx
详细日志
[root@computer_name ~]# rpm -qa nginx
[root@computer_name ~]# yum -y list nginx
Loaded plugins: langpacks
Available Packages
nginx.x86_64 1:1.6.3-9.el7 epel
[root@computer_name ~]# yum -y install nginx
Loaded plugins: langpacks
base | 3.6 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.6.3-9.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.6.3-9.el7 for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: gd for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: GeoIP for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: 1:nginx-1.6.3-9.el7.x86_64
--> Running transaction check
---> Package GeoIP.x86_64 0:1.5.0-9.el7 will be installed
---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
---> Package gperftools-libs.x86_64 0:2.4-7.el7 will be installed
--> Processing Dependency: libunwind.so.8()(64bit) for package: gperftools-libs-2.4-7.el7.x86_64
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.6.3-9.el7 will be installed
--> Running transaction check
---> Package libXpm.x86_64 0:3.5.11-3.el7 will be installed
---> Package libunwind.x86_64 2:1.1-5.el7_2.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
nginx x86_64 1:1.6.3-9.el7 epel 508 k
Installing for dependencies:
GeoIP x86_64 1.5.0-9.el7 base 709 k
gd x86_64 2.0.35-26.el7 base 146 k
gperftools-libs x86_64 2.4-7.el7 base 272 k
libXpm x86_64 3.5.11-3.el7 base 54 k
libunwind x86_64 2:1.1-5.el7_2.2 updates 56 k
libxslt x86_64 1.1.28-5.el7 base 242 k
nginx-filesystem noarch 1:1.6.3-9.el7 epel 15 k
Transaction Summary
===============================================================================================================================================================================================
Install 1 Package (+7 Dependent packages)
Total download size: 2.0 M
Installed size: 5.6 M
Downloading packages:
(1/8): GeoIP-1.5.0-9.el7.x86_64.rpm | 709 kB 00:00:00
(2/8): gd-2.0.35-26.el7.x86_64.rpm | 146 kB 00:00:00
(3/8): libXpm-3.5.11-3.el7.x86_64.rpm | 54 kB 00:00:00
(4/8): gperftools-libs-2.4-7.el7.x86_64.rpm | 272 kB 00:00:00
(5/8): libunwind-1.1-5.el7_2.2.x86_64.rpm | 56 kB 00:00:00
(6/8): libxslt-1.1.28-5.el7.x86_64.rpm | 242 kB 00:00:00
(7/8): nginx-1.6.3-9.el7.x86_64.rpm | 508 kB 00:00:00
(8/8): nginx-filesystem-1.6.3-9.el7.noarch.rpm | 15 kB 00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.3 MB/s | 2.0 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 2:libunwind-1.1-5.el7_2.2.x86_64 1/8
Installing : gperftools-libs-2.4-7.el7.x86_64 2/8
Installing : 1:nginx-filesystem-1.6.3-9.el7.noarch 3/8
Installing : libXpm-3.5.11-3.el7.x86_64 4/8
Installing : gd-2.0.35-26.el7.x86_64 5/8
Installing : libxslt-1.1.28-5.el7.x86_64 6/8
Installing : GeoIP-1.5.0-9.el7.x86_64 7/8
Installing : 1:nginx-1.6.3-9.el7.x86_64 8/8
Verifying : GeoIP-1.5.0-9.el7.x86_64 1/8
Verifying : 1:nginx-1.6.3-9.el7.x86_64 2/8
Verifying : libxslt-1.1.28-5.el7.x86_64 3/8
Verifying : libXpm-3.5.11-3.el7.x86_64 4/8
Verifying : 1:nginx-filesystem-1.6.3-9.el7.noarch 5/8
Verifying : gd-2.0.35-26.el7.x86_64 6/8
Verifying : gperftools-libs-2.4-7.el7.x86_64 7/8
Verifying : 2:libunwind-1.1-5.el7_2.2.x86_64 8/8
Installed:
nginx.x86_64 1:1.6.3-9.el7
Dependency Installed:
GeoIP.x86_64 0:1.5.0-9.el7 gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.4-7.el7 libXpm.x86_64 0:3.5.11-3.el7 libunwind.x86_64 2:1.1-5.el7_2.2
libxslt.x86_64 0:1.1.28-5.el7 nginx-filesystem.noarch 1:1.6.3-9.el7
Complete!
[root@computer_name ~]# rpm -qa nginx
nginx-1.6.3-9.el7.x86_64
[root@computer_name ~]#
安装后的位置
/etc/nginx
基本操作
运行
(任意一个目录下)执行命令nginx即可。
[root@computer_name nginx]# nginx
[root@computer_name nginx]#
然后即可远程访问 http://host_name/,然后会打开nginx自带的缺省/示例文件。
其他命令
包括:
- nginx -s stop — fast shutdown
- nginx -s quit — graceful shutdown
- nginx -s reload — reloading the configuration file
- nginx -s reopen — reopening the log files
配置文件
nginx.conf
文件位置:/etc/nginx/nginx.conf
[root@computer_name nginx]# ll
total 68
drwxr-xr-x 2 root root 4096 Jun 1 01:58 conf.d
...
-rw-r--r-- 1 root root 1516 Jun 1 01:56 nginx.conf
-rw-r--r-- 1 root root 2656 Jun 1 01:58 nginx.conf.default
...
-rw-r--r-- 1 root root 3610 Jun 1 01:58 win-utf
[root@computer_name nginx]# pwd
/etc/nginx
[root@computer_name nginx]#
缺省配置
安装后配置文件的内容:
[root@computer_name nginx]# cat nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
[root@computer_name nginx]#
示例页面
在缺省配置中有个web示例目录:
root /usr/share/nginx/html;
该目录内容:
[root@computer_name nginx]# cd /usr/share/nginx/
[root@computer_name nginx]# ll
total 4
drwxr-xr-x 2 root root 4096 Jul 27 10:54 html
[root@computer_name nginx]# cd html/
[root@computer_name html]# ll
total 20
-rw-r--r-- 1 root root 3650 Jun 1 01:56 404.html
-rw-r--r-- 1 root root 3693 Jun 1 01:56 50x.html
-rw-r--r-- 1 root root 3700 Jun 1 01:56 index.html
-rw-r--r-- 1 root root 368 Jun 1 01:56 nginx-logo.png
-rw-r--r-- 1 root root 2811 Jun 1 01:56 poweredby.png
[root@computer_name html]#
缺省页面的内容:
[root@computer_name html]# pwd
/usr/share/nginx/html
[root@computer_name html]# ll
total 20
-rw-r--r-- 1 root root 3650 Jun 1 01:56 404.html
-rw-r--r-- 1 root root 3693 Jun 1 01:56 50x.html
-rw-r--r-- 1 root root 3700 Jun 1 01:56 index.html
-rw-r--r-- 1 root root 368 Jun 1 01:56 nginx-logo.png
-rw-r--r-- 1 root root 2811 Jun 1 01:56 poweredby.png
[root@computer_name html]# cat index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Page for the Nginx HTTP Server on Fedora</title>
...
</head>
<body>
<h1>Welcome to <strong>nginx</strong> on Fedora!</h1>
<div class="content">
<p>This page is used to test the proper operation of the
<strong>nginx</strong> HTTP server after it has been
installed. If you can read this page, it means that the
web server installed at this site is working
properly.</p>
...
</div>
</body>
</html>
[root@computer_name html]#
日志文件
这些日志文件有助于定位问题,类似于tomcat的logs目录下的文件。
[root@computer_name nginx]# ll
total 8
-rw-r--r-- 1 root root 869 Jul 29 09:12 access.log
-rw-r--r-- 1 root root 251 Jul 29 09:12 error.log
[root@computer_name nginx]# pwd
/var/log/nginx
[root@computer_name nginx]#
文件内容示例:
[root@computer_name nginx]# cat error.log
2016/07/29 09:12:14 [error] 19462#0: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 180.173.67.64, server: _, request: "GET /favicon.ico HTTP/1.1", host: "47.88.194.220", referrer: "http://server_host/"
[root@computer_name nginx]# cat access.log
client_ip_address - - [29/Jul/2016:09:12:13 +0800] "GET / HTTP/1.1" 200 3700 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" "-"
client_ip_address - - [29/Jul/2016:09:12:14 +0800] "GET /poweredby.png HTTP/1.1" 200 2811 "http://server_host/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" "-"
client_ip_address - - [29/Jul/2016:09:12:14 +0800] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://server_host/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" "-"
client_ip_address - - [29/Jul/2016:09:12:14 +0800] "GET /favicon.ico HTTP/1.1" 404 3650 "http://server_host/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" "-"
[root@computer_name nginx]#
从日志中可以看到哪个主机访问哪个页面的问题。
静态页面
这一节都是本文一开头提到的官网的内容。——鉴于学习笔记的性质,这里的描述可能存在上下文不完整的情况。
http->server->location
一个http下面可以有多个server,它们以端口号和server名称进行区分。为便于说明,把缺省配置再拷贝一份(删除了一些内容,仅保留http->server->location的结构):
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
location / {
}
示例使用场景
比如现在有两类静态页面,在服务器上可以放到两个目录下面。
/data/www:which may contain HTML files
/data/images:containing images
web服务器在取文件的时候,会根据请求(URL)来决定从什么地方取。在配置文件中,http->server下面要定义两个location,如下一节描述。
匹配
将请求头部的URI和location进行比较,并采用最长匹配原则。示例:
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
几种匹配的例子:
- http://localhost/images/example.png:匹配的本地目录为 /data/images/example.png。——URI是/images/example.png,最长匹配的location是/images/,所以把这个location的root(即/data)加到URI,最后形成的文件位置是:/data/images/examples.png
- http://localhost/some/example.html:匹配结果为/data/www/some/example.html。——URI是/some/example.html,和location /images/不匹配,而和location /匹配,所以添加/data/www到URI的前面,形成/data/www/some/example.html。
root
如果location中没有定义root,那么就使用server上下文定义的root。
代理服务器
思路
nginx的一个常见使用场景是作为代理服务器,即收到请求后,把请求转发给被代理的服务器;然后从后者接收到响应结果,再返回给客户端。
考虑如下场景:
- 对于images请求,全部从本地取文件;
- 对于其他请求,则由代理服务器处理。
这相当于是有两个server。在这里,两个server都定义到一个nginx实例中。
对于代理服务器,增加一个server:
server {
listen 8080;
root /data/up1;
location / {
}
}
直接将本地的tomcat作为代理
配置文件如下:
server {
location / {
proxy_pass http://localhost:8080/;
}
在缺省的nginx配置文件中,只加了proxy_pass这一行。
使用另外一台tomcat服务器
把上面的配置文件中的代理服务器改成专门的tomcat服务器,然后nginx -s reload,则nginx使用tomcat服务器来处理8080请求。
proxy_pass http://another_server_host:8080/;
此时,可以将nginx所在的机器来处理静态页面。——内容待补充。