1.随便启动一个nginx实例,只是为了复制出配置
docker run -p80:80 --name nginx -d nginx:1.10
2.将容器内的配置文件拷贝到/mydata/nginx/conf/ 下
3.终止原容器
docker stop nginx
4.执行命令删除原容器:
docker rm nginx
5.创建新的Nginx,执行以下命令
6.设置开机启动nginx
docker update nginx --restart=always
7.创建“/mydata/nginx/html/index.html”文件,测试是否能够正常访问
echo '<h2>hello nginx!</h2>' >index.html
访问:http://nginx所在主机的IP:80/index.html
8.nginx配置文件
(2)nginx.config
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;
upstream venusmall{
# 88是网关
server 192.168.56.1:88;
}
include /etc/nginx/conf.d/*.conf; # 包含了哪些配置文件
}
server {
listen 80;
#匹配的host,如果都没有匹配,默认走第一个server模块
server_name gulimall.com *.gulimall.com;
location /static {
root /usr/share/nginx/html;
}
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#会找到upstream的venusmall的设定
location / {
proxy_pass http://venusmall;
proxy_set_header Host $host; #nginx反向代理时,需要把host重新设置
}
include /etc/nginx/conf.d/*.conf; # 包含了哪些配置文件
}
9.官网
nginx documentationhttps://nginx.org/en/docs/10.设置反向代理
修改hosts文件或者购买域名,将域名与 nginx所在的服务器ip相对应上
添加nginx的server模块,设置监听端口,server_name, location,配置网关负载均衡,见上图。
11.动静分离配置
#http://venusmall.com/static/index/img/zhongchou.png
#/usr/share/nginx/html/static/index/img/zhongchou.png 就是最终的路径
location /static/ {
root /usr/share/nginx/html;
}