nginx的介绍
Nginx 是一款高性能的 http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 ,nginx 能够支撑5 万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。
Nginx应用场景
- http 服务器。Nginx 是一个 http 服务可以独立提供 http 服务。可以做网页静态服务器。
- 虚拟主机。可以实现在⼀台服务器虚拟出多个⽹站。例如个⼈⽹站使⽤的虚拟主机。
- 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用 nginx 做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
安装Nginx(docker安装)
1. 搜索nginx镜像
docker search nginx
2. 拉取nginx镜像
docker pull nginx
3. 创建容器,设置端口映射、目录映射
# 在/root目录下创建nginx目录用于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf文件,粘贴下面内容
vim nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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 on;
include /etc/nginx/conf.d/*.conf;
}
~/nginx/conf.d/80.conf
server {
listen 80; # 监听的端口
server_name localhost; # 域名或ip
location / { # 访问路径配置
root /usr/share/nginx/html;# 根目录
index index.html index.htm; # 默认首页
}
error_page 500 502 503 504 /50x.html; # 错误页面
location = /50x.html {
root html;
}
}
在~/nginx目录下安装
docker run -id --name=c_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
参数说明
- -p 80:80:将容器的 80端口映射到宿主机的 80 端口
- -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机当前目录下的
/conf/nginx.conf 挂载到容器的 :/etc/nginx/nginx.conf 配置目录 - -v $PWD/logs:/var/log/nginx:将主机当前目录下的 logs 目录挂载到容器
- 的/var/log/nginx 日志目录
使用外部机器访问nginx
192.168.xx.xx访问会出现404因为没有加载html页面,现在nginx就装好了