#拉取nginx
docker pull nginx:latest
#启动容器
#docker run -d -p 9090:9090 --name nginx01 -v ~/app/nginx:/usr/local/nginx nginx:latest
docker run --name nginx -p 9090:9090 -d nginx
#进入容器内部
docker exec -it nginx bash
#nginx.conf配置文件在
cd /etc/nginx/
#使用vim nginx.conf 或者vi nginx.conf
apt-get update
apt-get install vim
vim nginx.conf
#重启nginx
docker restart nginx
docker stop nginx
docker start nginx
#查看nginx.conf
cat /etc/nginx/nginx.conf
cat /etc/nginx/conf.d/*.conf;
#需要自己创建下文件nginx.conf
#/root/docker/nginx
#/root/docker/nginx/conf.d
docker cp 6152:/etc/nginx/nginx.conf /root/docker/nginx/nginx.conf
docker cp 6152:/var/log/nginx /root/docker/nginx/log
docker cp 6152:/etc/nginx/conf.d/default.conf /root/docker/nginx/conf.d/default.conf
docker run --name nginx03 -p 9093:9090 -v /root/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/docker/nginx/log:/var/log/nginx -v /root/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d nginx
docker run --name nginx9090 -p 9090:9090 \
-v /root/docker/nginx9090/nginx.conf:/etc/nginx/nginx.conf \
-v /root/docker/nginx9090/html:/usr/share/nginx/html \
-v /root/docker/nginx9090/log:/var/log/nginx \
# -v /root/docker/nginx9090/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-d nginx
#--name 给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器
#-p 映射端口,将docker宿主机的9090端口和容器的9090口进行绑定
#-v 挂载文件用的,第一个-v 表示将你本地的nginx.conf覆盖你要起启动的容器的nginx.conf文件
#第二个表示将日志文件进行挂载,就是把nginx服务器的日志写到你docker宿主机的/root/docker/nginx/log/下面
#第三个-v 表示的和第一个-v意思一样的。
#-d 表示启动的是哪个镜像
docker run --name nginx9090 -p 9090:9090 \
-v /root/docker/nginx9090/nginx.conf:/etc/nginx/nginx.conf \
-v /root/docker/nginx9090/html:/usr/share/nginx/html \
-v /root/docker/nginx9090/log:/var/log/nginx \
-d nginx
#--name 给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器
#-p 映射端口,将docker宿主机的9090端口和容器的9090口进行绑定
#-v 挂载文件用的,第一个-v 表示将你本地的nginx.conf覆盖你要起启动的容器的nginx.conf文件
#第二个表示将日志文件进行挂载,就是把nginx服务器的日志写到你docker宿主机的/root/docker/nginx/log/下面
#第三个-v 表示的和第一个-v意思一样的。
#-d 表示启动的是哪个镜像
记得注释掉最后一行# include /etc/nginx/conf.d/*.conf;
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
server {
listen 9090;
#listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# include /etc/nginx/conf.d/*.conf;
}
}
➜ static pwd
/root/docker/nginx9090/html/static
➜ static ls
index.html
➜ static cat index.html
<html>hello world static</html>
添加了修改location为
location /static/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
location / {
proxy_pass http://10.0.4.17:8070;
}
完整版如下
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
server {
listen 9090;
#listen [::]:80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
#location / {
# root /usr/share/nginx/html;
#index index.html index.htm;
#}
location /static/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
location / {
proxy_pass http://10.0.4.17:8070;
}
# error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# include /etc/nginx/conf.d/*.conf;
}
}
可以看到日期比北京时间晚八个小时
而下面的为什么时间正常的,那是因为下面修改了
#参考https://blog.csdn.net/youcijibi/article/details/82823047
docker exec -it nginx9090 /bin/bash
root@48d2cfb3bb2b:/# date -R
Fri, 25 Mar 2022 10:19:18 +0000
root@48d2cfb3bb2b:/# cat /etc/timezone
Etc/UTC
root@48d2cfb3bb2b:/# exit
#时间复制到容器内
docker cp /usr/share/zoneinfo/Asia/Shanghai nginx9090:/etc/localtime
docker exec -it nginx9090 /bin/bash
echo "Asia/Shanghai" >> /etc/timezone
#时间正常了
root@48d2cfb3bb2b:/# date -R
Fri, 25 Mar 2022 18:33:23 +0800
root@48d2cfb3bb2b:/# cat /etc/timezone
Etc/UTC
Asia/Shanghai
root@48d2cfb3bb2b:/#
为什么我要配置动静分离
因为我安装过一个Nginx,其中静态文件存储一些Live2d的东西,不能随便修改,添加反向代理地址无效访问不到
艹!我又试了一下,原谅我技术没学到位
只需要nginx.conf修改为如下即可
主要是要这样写才行 proxy_pass http://10.0.4.17:8070/;
我当初漏掉了端口后面的/ 造成了访问不到,那么动静分离有
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# location /static/ {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
location /abc/ {
proxy_pass http://10.0.4.17:8070/;
}
location /def {
proxy_pass http://10.0.4.17:8080/;
}
更多Nginx 只是可以参考我的
Nginx笔记
当时在服务器布了一个系统,本来准备用Nginx反向代理的,结果当时试了一下不行,就是少了一个/ ,就愣是用tomcat端口直接暴露了。
具体可以看下我下面的文章
花了两天做了一个随机点菜系统