Nginx - 配置及反向代理
目录
1 Nginx 安装与启动
1.1 什么是Nginx
Nginx
是一款高性能的http
服务器 / 反向代理服务器及电子邮件(IMAP/POP3)
代理服务器。
Nginx
应用场景:
http
服务器。Nginx
是一个http
服务器可以独立提供http
服务,可以做网⻚静态服务器。- 虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
- 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用
nginx
做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
1.2 安装 Nginx
这里在CentOS 7环境下使用docker
安装nginx
- 搜索
nginx
镜像
docker search nginx
- 拉取
nginx
镜像
docker pull nginx
- 创建容器,设置端口映射、目录映射
# 在/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_l