1.nginx
1.yum安装nginx
1.配置nginx的yum源
关闭防火墙与selinux
#配置nginx的yum源官网
http://nginx.org/en/linux_packages.html
#配置稳定版本(将以下粘贴到创建的文件里)
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@localhost ~]# yum -y install nginx
2.查看nginx版本
#启动nginx(关闭防火墙、selinux)
[root@localhost ~]# nginx
[root@localhost ~]# systemctl start nginx
#停止nginx
[root@localhost ~]# nginx -s stop
[root@localhost ~]# systemctl stop nginx
#查看nginx版本(虽然$?=0但是为错误输出)
[root@localhost ~]# nginx -V
2.nginx配置文件
1.全局参数
参数位置:/etc/nginx/nginx.conf
user nginx;
#设置nginx启动进程的数量,一般设置与逻辑CPU数量相同
worker_processes auto;
#指定报错日志
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
#设置一个进程的最大连接数
events {
worker_connections 1024;
}
#HTTP相关服务设置
http {
include /etc/nginx/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;
tcp_nopush on;
#设置长连接的超市时间
keepalive_timeout 65;
#是否开启gzip压缩
gzip on;
include /etc/nginx/conf.d/*.conf;
}
2.检测nginx配置文件是否正确
[root@localhost ~]# nginx -t
[root@localhost ~]# mkdir -p /tmp/nginx
3.nginx虚拟服务配置
1.基于域名的虚拟主机
#配置本地解析的位置
c:\windows\System32\drivers\etc
#配置通过域名区分的虚拟机(在http下添加以下内容)
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name takehaye.test.com;
location / {
root /usr/share/nginx/html/;
index index.html;
}
}
server {
listen 80;
server_name yhb.test.com;
location / {
root /root;
index index.html;
}
}
}
注意:"{}"要前后呼应
#在root目录下创建index.html
[root@localhost ~]# echo "22222222" > index.html
#在/usr/share/nginx/html/创建index.html文件
[root@localhost ~]# echo "111111111111" > /usr/share/nginx/html/index.html
#分别以两个域名访问
2.基于IP的虚拟主机
#添加一个IP地址
[root@localhost ~]# ip addr add dev ens33 192.168.222.140/24
#配置nginx的配置文件
server {
listen 192.168.222.131:80;
server_name takehaye.test.com;
location / {
root /usr/share/nginx/html/;
index index.html;
}
}
server {
listen 192.168.222.140:80;
server_name yhb.test.com;
location / {
root /root;
index index.html;
}
}
}
#在浏览器输入两个不同的网站
3.基于端口的虚拟主机
#更改nginx配置文件
server {
listen 8080;
server_name takehaye.test.com;
location / {
root /usr/share/nginx/html/;
index index.html;
}
}
server {
listen 80;
server_name yhb.test.com;
location / {
root /root;
index index.html;
}
}
}
4.使用limit_rate限制客户端传输速度
#编辑nginx配置文件
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k; #对每个连接的限速为2k/s
}