#运行用户,默认即是nginx,可不设置
#user nobody;
#nginx进程,一般设置为和cpu核数一样
worker_processes 1;
#;单个后台worker process进程的最大并发链接数
events {
worker_connections 1024;
}
http {
#文件扩展名与类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#开启高效传输模式
sendfile on;
#连接超时时间,单位是秒
keepalive_timeout 65;
server {
#监听端口
listen 80;
#映射域名
server_name www.abc.com;
#可以实现多种编码识别
charset utf-8;
#nginx添加自定义header.表明,该资源可以被任意外域访问,用作跨域请求使用
add_header Access-Control-Allow-Origin *;
location / {
#请求转向mysvr 定义的服务器列表
proxy_pass http://192.168.126.1:9000/;
#用来在后端获取用户发送过来的请求头
proxy_set_header Host $host;
#用来获取客户端的IP
proxy_set_header Remote_Addr $remote_addr;
#在后端这样获取:request.getAttribute("X-real-ip")
proxy_set_header X-Real-IP $remote_addr;
#后端通过request.getAttribute("X-Forwarded-For")获得的将会是客户端ip和第一台nginx的ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#访问http://192.168.126.1:9000/的路径会被映射到index.html
index index.html;
}
location ~ ^/(hryfilefront) {
proxy_pass http://192.168.126.1:9000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location ~ ^/(hryfile) {
proxy_pass http://192.168.126.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name manage.abc.com ;
charset utf-8;
location ~ ^/(manage) {
proxy_pass http://192.168.126.1:9002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(hryfile) {
proxy_pass http://192.168.126.1:9002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(oauth) {
proxy_pass http://192.168.126.1:9001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(hryfilefront) {
proxy_pass http://192.168.126.1:9002;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location ~ ^/(hryfile) {
proxy_pass http://192.168.126.1:9002;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location ~ ^/(admin) {
root /usr/share/nginx/html/admin;
index index.html;
}
}
server {
listen 80;
server_name hq.abc.com ;
charset utf-8;
location / {
proxy_pass http://192.168.126.1.25:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
安装命令解析
docker pull nginx:1.12.2
docker run -itd --restart=unless-stopped -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone -p $1:80:80 --name nginx -v /data/yjs/nginx/html:/usr/share/nginx/html -v /data/yjs/nginx/logs:/var/log/nginx -v /data/yjs/nginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx:1.12.2
解析后:
#拉取版本1.12.2的nginx
docker pull nginx:1.12.2
#-i: 以交互模式运行容器,通常与 -t 同时使用;
#-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
#-d: 后台运行容器,并返回容器ID;
#通过--restart选项,可以设置容器的重启策略,以决定在容器退出时Docker守护进程是否重启刚刚退出的容器。
#unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
docker run -itd --restart=unless-stopped
# 同步时区
-v /etc/localtime:/etc/localtime
-v /etc/timezone:/etc/timezone
#开放端口,并映射到本地的 80 端口。
-p $1:80:80 --name nginx
#挂载到绝对路径下
-v /data/yjs/nginx/html:/usr/share/nginx/html
-v /data/yjs/nginx/logs:/var/log/nginx
-v /data/yjs/nginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx:1.12.2
参考资源路径:
nginx add_header指令使用方法
nginx proxy_set_header设置、自定义header
nginx location正则写法
nginx配置文件nginx.conf超详细讲解
注意事项:
- 配置路径要为绝对路径
- 上面的域名要想可以访问,要使用域名信息备案过的域名方可