1、配置
#一个server就是一个虚拟主机
server {
listen 80;//默认端口
server_name 222.143.39.129;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;//安装目录下面的相对目录,绝对路径也可以
index index.html index.htm;//欢迎页
}
#配置的错误界面,也可以不配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
静态资源文件(例如图片服务器)
通过端口区分不同的虚拟主机
server {
listen 80;//默认端口
server_name 222.143.39.129;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;//安装目录下面的相对目录,绝对路径也可以
index index.html index.htm;//欢迎页
}
server {
listen 81;//默认端口
server_name 222.143.39.129;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;//安装目录下面的相对目录,绝对路径也可以
index index81.html index.htm;//欢迎页
}
通过域名区分(一个ip挂多个域名,但是一个域名只能对应一个IP):
server {
listen 80;//默认端口
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;//安装目录下面的相对目录,绝对路径也可以
index index.html index.htm;//欢迎页
}
server {
listen 80;//默认端口
server_name www.163.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;//安装目录下面的相对目录,绝对路径也可以
index index.html index.htm;//欢迎页
}
反向代理:nginx请求的转发,转发至应用服务器。
配置
upstream sina{
server 192.168.25.148:8080
}
server {
listen 80;//默认端口
server_name www.sina.com;
location / {
#root html;#root代表直接访问的根路径
proxy_pass http://sina;#proxy_pass映射到其他的应用服务器,前面应声明应用服务器地址
index index.html index.htm;//欢迎页
}
upstream baidu{
server 192.168.25.148:8080
}
server {
listen 80;//默认端口
server_name www.baidu.com;
location / {
#root html;#root代表直接访问的根路径
proxy_pass http://baidu;#proxy_pass映射到其他的应用服务器,前面应声明应用服务器地址
index index.html index.htm;//欢迎页
}
负载均衡:
upstream baidu{
server 192.168.25.148:8080;
server 192.168.25.148:8081 weight=2;//默认是1,整数
}
server {
listen 80;//默认端口
server_name www.baidu.com;
location / {
#root html;#root代表直接访问的根路径
proxy_pass http://baidu;#proxy_pass映射到其他的应用服务器,前面应声明应用服务器地址
index index.html index.htm;//欢迎页
}