docker安装nginx1.27.0
关于拉取不到镜像问题可以到这篇文章进行镜像配置
一、docker拉取nginx1.27.0镜像
docker pull nginx:1.27.0
二、创建映射容器的文件目录
mkdir -p -m 777 /mydata/nginx/conf/conf.d
mkdir -p -m 777 /mydata/nginx/log
mkdir -p -m 777 /mydata/nginx/html
三、创建文件default.conf放入/mydata/nginx/conf/conf.d
server {listen 80;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##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}
}
四、创建文件nginx.conf放入/mydata/nginx/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;include /etc/nginx/conf.d/*.conf;
}
五、创建文件50x.html放入/mydata/nginx/html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
六、创建文件index.html放入/mydata/nginx/html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
七、执行docker命令
如果你的前端不同项目想配置别的端口,需要一开始就在这里预设好 例如 -p 10010:10010
docker run -p 80:80 --name nginx \
--restart=always \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/log:/var/log/nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-d nginx:1.27.0
八、访问ip地址即可看到界面
注意:如果我们自己的项目需要进行配置,因为在nginx里采用的是include模式,可以直接在default.conf里进行修改即可,或者在default.conf同目录新增一个自己的配置,例如my.conf
upstream backend{server 192.168.0.77:6060;keepalive 1000;}server {listen 10010;location / {root /usr/share/nginx/html/systemDist;index index.html index.htm;try_files $uri $uri/ /index.html;}location /api {proxy_pass http://backend;rewrite ^/api/(.*)$ /$1 break;}
}
例如有个system的服务,我需要部署前端到nginx里,那么10010就是访问前端的端口,也对应前面docker run中配置的映射宿主机端口,127.0.0.77:6060为后端的请求地址,/api用来处理跨域问题做代理转发,意思是将访问带了/api的请求全部转发到192.168.0.77:6060中去,并且转发的过程中去掉了api前缀。
刷新nginx配置:
docker exec -it nginx nginx -s reload