nginx 配置篇
简单版nginx虚拟主机配置
基于域名的虚拟主机
[root@localhost conf]# cat nginx.conf user nginx nginx; #用nginx去访问nginx worker_processes 1; #进程数,通常是CPU 乘 2 error_log /yellow.log/error.log crit; #crit 日志级别(严重) pid logs/nginx.pid; events { #事件 use epoll; #使用的IO模型 worker_connections 1024; #进程连接数 } http { include mime.types; default_type application/octet-stream; #nginx日志格式,也可以加错误日志,如果不加默认走上边全局的 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; #server是虚拟主机配置 server { listen 80; server_name www.huangxu.com huangxu.com; #第二个是别名 location / { root /yellow; #站点目录 index index.html index.htm; #首页文件 access_log /yellow.log/access_ye.log main ; #main 是上边调用的 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } } ######### server { listen 80; server_name www.huanghuang.com huanghuang.com; #第二个是别名 location / { root /huangxu; #站点目录 index index.html index.htm; #首页文件 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } } }
具体配置文件格式参照nginx/conf/下的nginx.conf.default 这个文件
由于虚拟主机过多我也可这样管理虚拟主机
mkdir /etc/opt/nginx/conf/extra
cp /etc/opt/nginx/conf/nginx.conf /etc/opt/nginx/conf/extra/nginx_vhosts.conf
[root@localhost conf]# cat nginx.conf
user nginx nginx; #用nginx去访问nginx
worker_processes 1; #进程数,通常是CPU 乘 2
error_log /yellow.log/error.log crit; #crit 日志级别(严重)
pid logs/nginx.pid;
events { #事件
use epoll; #使用的IO模型
worker_connections 1024; #进程连接数
}
http {
include mime.types;
default_type application/octet-stream;
#nginx日志格式,也可以加错误日志,如果不加默认走上边全局的
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
include extra/nginx_vhosts.conf;
}
用include 将所有的server虚拟主机引进来,通过文件的形式来管理虚拟主机
cd /etc/opt/nginx/conf/extra/nginx_vhosts.conf
[root@localhost extra]# cat nginx_vhosts.conf #server是虚拟主机配置 server { listen 80; server_name www.huangxu.com huangxu.com; #第二个是别名 location / { root /yellow; #站点目录 index index.html index.htm; #首页文件 access_log /yellow.log/access_ye.log main ; #main 是上边调用的 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } } ######### server { listen 80; server_name www.huanghuang.com huanghuang.com; #第二个是别名 location / { root /huangxu; #站点目录 index index.html index.htm; #首页文件 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } }
egrep -v "#|^$" nginx.conf > nginx.conf.bak 去掉空行并重定向到新的文件
mv nginx.conf.bak nginx.conf
mkdir /yellow 创建站点
chown -R nginx.nginx /yellow/ 使nginx有管理站点的权限
mkdir /yellow.log 创建站点日志
echo www > /yellow/index.html 创建站点访问首页
/etc/opt/nginx/sbin/nginx -t 检查语法
/etc/opt/nginx/sbin/nginx -s reload 平滑重启
lsof -i:80 检查端口
ps -ef | gerp nginx
wget http://192.168.1.104
cat index.html
wget http://www.huangxu.com
需要添加本地域名解析
C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
0.0.0.0 account.jetbrains.com
192.168.1.104 www.huangxu.com huangxu.com
在浏览器中输入域名,如果返回www则正确
如果再添加虚拟主机只需要在添加一个server标签即可,其他都与上边一样
[root@localhost huangxu]# ps -ef | grep nginx
root 38005 1 0 10:28 ? 00:00:00 nginx: master process /etc/opt/nginx/sbin/nginx
nginx 38450 38005 0 12:11 ? 00:00:00 nginx: worker process
root 38452 4286 0 12:11 pts/1 00:00:00 grep nginx
[root@localhost huangxu]# cat /etc/opt/nginx/conf/nginx.conf
user nginx nginx; #用nginx去访问nginx
worker_processes 1; #进程数,通常是CPU 乘 2
events { #事件
use epoll; #使用的IO模型
worker_connections 1024; #进程连接数
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#server是虚拟主机配置
server {
listen 80;
server_name www.huangxu.com huangxu.com; #第二个是别名
location / {
root /yellow; #站点目录
index index.html index.htm; #首页文件
}
error_page 500 502 503 504 /50x.html; #错误页面
location = /50x.html {
root html;
}
}
#########
server {
listen 80;
server_name www.huanghuang.com huanghuang.com; #第二个是别名
location / {
root /huangxu; #站点目录
index index.html index.htm; #首页文件
}
error_page 500 502 503 504 /50x.html; #错误页面
location = /50x.html {
root html;
}
}
}
基于端口的虚拟主机配置
访问时需要域名+端口
server {
listen 8080;
server_name www.huanghuang.com huanghuang.com; #第二个是别名
location / {
root /huangxu; #站点目录
index index.html index.htm; #首页文件
}
error_page 500 502 503 504 /50x.html; #错误页面
location = /50x.html {
root html;
}
}
基于ip的虚拟主机
需要每台虚拟主机都有个ip
创建虚拟ip: ifconfig eth0:192.168.1.111 netmask 255.255.255.0 up
访问例子:http://ip
server { listen 192.168.1.111:8080; server_name 192.168.1.111; location / { root /huangxu; #站点目录 index index.html index.htm; #首页文件 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } }
基于别名
别名可以设置多个,可以通过别名来判断后端机器的健康状况
server { listen 8080; server_name www.huanghuang.com huanghuang.com aaa.huang.com; 域名后的都是别名 location / { root /huangxu; #站点目录 index index.html index.htm; #首页文件 } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } }
负载均衡和反向代理参考文章
https://blog.csdn.net/wm_1991/article/details/51935273
基于keeplived的负载均衡
https://www.cnblogs.com/wang-meng/p/5861174.html‘
反向代理负载均衡