安装
这部分的官方文档: https://nginx.org/en/linux_packages.html
针对ubuntu,具体步骤为:
step-1
添加软件源将以下代码添加到/etc/apt/sources.list
deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx
其中codename
因ubuntu版本不同而不同。
Version | Codename | Supported Platforms |
---|---|---|
14.04 | trusty | x86_64, i386, aarch64/arm64 |
16.04 | xenial | x86_64, i386, ppc64el, aarch64/arm64 |
17.10 | artful | x86_64, i386 |
step-2
更新软件源
apt-get update
安装
apt-get install nginx
step-3
启动
nginx
默认使用80端口进行监听
两个简单的配置
缺省的情况下配置文件
nginx.conf
位于/usr/local/nginx/conf
、/etc/nginx
或者/usr/local/etc/nginx
静态文件服务器
在http块内部添加一个server,其中包含两个location:
http {
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
第一个location负责转发路径为/下面的所有请求到本地文件系统的/data/www
目录
第二个location负责转发路径为/images下面的所有请求到本地文件系统的/data
目录
当两个location的路径冲突时,按照下面的规则命中:
代理转发
代理转发的目的是将一个请求从nginx转发到另一个server进行实际处理,可能是另一个nginx server,也可能是其余的http服务进程。
简单的配置如:
server { # 静态文件服务器,实际处理请求
listen 8080; # 监听端口,不配置的情况下默认为80
root /data/www; # 默认网站根目录位置
location / {
}
}
server {
location / {
proxy_pass http://localhost:8080; # 宿主服务器地址
}
location ~ \.(gif|jpg|png)$ { # 这里使用正则匹配来处理所有的图片文件请求(也就是说这个server不仅仅转发代理,自己也作为静态文件服务器运行)
root /data/image;
}
}