简介
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日
Nginx 安装
- docker-compose安装
➜ ~ mkdir docker_nginx && cd docker_nginx
➜ ~ vim docker-compose.yml
// docker-compose.yml的内容
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 8087:80
// 保存退出,运行如下命令
➜ docker_nginx docker-compose up -d
Recreating nginx ... done
- 浏览器访问:http://localhost:8087
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JF0WI3g2-1602215019702)(nginx/image-20200915201202164.png)]
Nginx配置文件
➜ docker_nginx docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbe84a274189 daocloud.io/library/nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:8087->80/tcp nginx
➜ docker_nginx docker exec -it dbe bash
root@dbe84a274189:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@dbe84a274189:/# cd etc/nginx/
root@dbe84a274189:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@dbe84a274189:/etc/nginx# cat nginx.conf
- nginx.conf 内容
// worker_processes和worker_connections 决定了nginx的并发能力
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;
// 注意,/etc/nginx/conf.d下.conf文件都会加载
include /etc/nginx/conf.d/*.conf;
}
- default.conf 的内容
配置文件中除了注释的内容,最重要的就是server 模块
root@dbe84a274189:/etc/nginx# cd conf.d/
root@dbe84a274189:/etc/nginx/conf.d# ls
default.conf
root@dbe84a274189:/etc/nginx/conf.d# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
- 修改docker-compose.yml
关联本地目录conf.d 到nginx配置文件目录
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 8087:80
volumes:
- ./conf.d:/etc/nginx/conf.d
- 添加default.conf
需要添加default.conf,否则会访问不到nginx的首页
➜ docker_nginx ls
conf.d docker-compose.yml
➜ docker_nginx cd conf.d
➜ conf.d vim default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
- 重启nginx
➜ conf.d docker-compose down
➜ conf.d docker-compose build
➜ conf.d docker-compose up -d
Nginx功能
反向代理
正向代理
- 客户端自己设置
- 客户端了解背后的代理和目标服务器
- 对目标服务器访问隐藏了客户端ip
反向代理
- 配置是在目标服务器端
- 客户端不知道访问到的是哪一台目标服务器
- 对客户端隐藏了目标服务器的ip
配置location
优先级关系: (location = ) > (location /xxx/yyy/zzz) > (location ^~) > (location ,*) > (location /起始路径) > (location /)
- 注释掉之前的location 模块,添加如下模块,让其跳转到百度首页
location / {
proxy_pass https://www.baidu.com;
}
- 其他配置
# 1. = 匹配
location = / {
# 精准匹配,主机名后面不能带任何的字符串
}
# 2. 通用匹配
location /xxx{
# 匹配所有以/xxx开头的路径
}
# 3. 正则匹配
location ~/xxx {
# 匹配所有以/xxx开头的路径
}
# 4. 匹配开头路径
location ^~/images/ {
# 匹配所有以 /images 开头的路径
}
# 5. ~* \.(gif|jpg|png)$ {
# 匹配以gif或者jpg或者png为结尾的路径
}
负载均衡
- 轮询(默认)
- 权重(weight=2,默认为1)
- ip_hash(按照客户端IP地址的分配方式,可以确保相同客户端的请求一直发送到相同的服务器。配置了,其他配置都没效果了)
//配置要映射的服务器
upstream my_upstream {
ip_hash;
server 192.168.90.01:8081 weight=2;;
server 192.168.90.02:8082 weight=3;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://my_upstream;
}
}
Nginx 常见命令
-
启动
- /usr/sbin/nginx
- nginx
-
停止
nginx -s stop
-
查看nginx进程
ps -ef | grep nginx
-
重新加载配置文件
nginx -s reload
-
检配置文件是否正确
nginx -t
-
查看nginx 版本
nginx -v