了解nginx的配置文件内容
我的nginx是编译安装的
/etc/nginx/nginx.conf
# 全局参数设置
user nginx; #指定用户
worker_processes 1; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
error_log logs/error.log; #指定错误日志
worker_rlimit_nofile 10240; #设置一个nginx进程能打开的最大文件数
pid /var/run/nginx.pid;
events {
worker_connections 1024; #设置一个进程的最大并发连接数
}# http 服务相关设置
http {
include mime.types; #数据类型
default_type application/octet-stream;
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; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
gzip on; #是否开启gzip压缩,将注释去掉开启
keepalive_timeout 65; #设置长连接的超时时间
# 虚拟服务器的相关设置
server {
listen 80; #设置监听的端口
server_name localhost; #设置绑定的主机名、域名或ip地址
# charset koi8-r; # 设置编码字符
charset utf-8;
location / {
root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建
index index.html index.htm; #设置默认打开的文档
}
error_page 500 502 503 504 /50x.html; #设置错误信息返回页面
location = /50x.html {
root html; #这里的绝对位置是/usr/local/nginx/html
}
}
}
nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
基于域名的虚拟主机
修改配置文件 在http块内添加
server {
listen 80;
server_name www.ceshi1.com;
location / {
root /var/www/nginx1;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 80;
server_name www.ceshi2.com;
location / {
root /var/www/nginx2;
index index.html index.htm;
limit_rate 2k;
}
}
listen 80; 端口80
server_name www.ceshi1.com; 设置绑定的主机名、域名或ip地址
location / {
root /var/www/nginx1; 设置服务器默认网站的根目录位置,需要手动创建
index index.html index.htm; 设置默认打开的文档
limit_rate 2k;
}
为域名www.ceshi1.com 的虚拟机,创建index文件
[root@b-2 ~]# mkdir -p /var/www/nginx1
[root@b-2 ~]# vim /var/www/nginx1/index.html
hello ceshi1
为域名www.ceshi2.com 的虚拟机,创建index文件
[root@b-2 ~]# mkdir -p /var/www/nginx2
[root@b-2 ~]# vim /var/www/nginx2/index.html
hello ceshi2
查看配置文件有无语法错误,重新加载配置文件
[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@b-2 ~]# nginx -s reload
在Windows客户端配置解析
win+r drivers
找到etc文件夹
记事本打开hosts文件 添加域名解析即可
如果无法保存,详看https://www.jianshu.com/p/7bbb309eae6e
测试访问
基于ip的虚拟主机
我们已经添加过测试文件index.html
我们先添加一个临时ip
[root@localhost ~]# yum -y install net-tools
[root@localhost ~]# ifconfig ens33:1 192.168.126.149/24
修改配置文件
检查并重新加载配置文件
[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@b-2 ~]# nginx -s reload
测试访问
基于端口的虚拟主机
vim /etc/nginx/nginx.conf
server {
listen 80;
server_name 192.168.126.141;
location / {
root /var/www/nginx1;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 8080;
server_name 192.168.126.141;
location / {
root /var/www/nginx2;
index index.html index.htm;
limit_rate 2k;
}
}
[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@b-2 ~]# nginx -s reload
测试访问