JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

一步一步教你Docker 安装 Nginx

wys521 2024-09-09 11:55:49 精选教程 29 ℃ 0 评论

Docker 安装 Nginx

安装基本步骤

1、查找 Nginx 镜像

docker search nginx

 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11117 [OK] 
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1571 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 696 [OK]
jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 494 [OK]
webdevops/php-nginx Nginx with PHP-FPM 123 [OK]
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 92 [OK]
bitnami/nginx Bitnami nginx Docker Image 64 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 57 
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 49 [OK]
zabbix/zabbix-web-nginx-pgsql Zabbix frontend based on Nginx with PostgreS… 30 [OK]
tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 24 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 17 
nginxdemos/hello NGINX webserver that serves a simple page co… 13 [OK]
wodby/drupal-nginx Nginx for Drupal container image 12 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 12 [OK]
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 10 
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 7 
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 4 
1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
mailu/nginx Mailu nginx frontend 3 [OK]
travix/nginx NGinx reverse proxy 2 [OK]
toccoag/openshift-nginx Nginx reverse proxy for Nice running on same… 1 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]
wodby/nginx Generic nginx 0 [OK]
 

2、下载 Nginx 镜像(默认nginx 镜像)

docker pull nginx

Using default tag: latest
latest: Pulling from library/nginx
f7e2b70d04ae: Pull complete 
08dd01e3f3ac: Pull complete 
d9ef3a1eb792: Pull complete 
Digest: sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
Status: Downloaded newer image for nginx:latest
 

3、查看本地的 Nginx 镜像

docker images nginx

 REPOSITORY TAG IMAGE ID CREATED SIZE
 nginx latest 881bd08c0b08 2 weeks ago 109MB
 

Docker 运行 Nginx

先要把 nginx 的文件目录,映射到我们服务器目录

1、创建目录

mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

2、在服务器路径[/root/nginx/conf]下配置nginx.conf

vi /root/nginx/conf/nginx.conf

 #user nobody;
worker_processes 1;
error_log logs/error.log;
events {
 worker_connections 1024;
}
http {
 include mime.types;
 default_type application/octet-stream;
 access_log logs/access.log ;
 sendfile on;
 server {
 listen 80;
 server_name localhost;
 location / {
 root www;
 index index.html index.htm;
 }
 
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
 }
}
 

3、在服务器路径[/root/nginx/www]编译index.html页面

vi /root/nginx/www/index.html

<!DOCTYPE html> 
<html> 
<head> 
<title> Welcome to nginx!</title> 
<style> 
 body {
 width: 35em;
 margin: 0 auto;
 font-family: Tahoma, Verdana, Arial, sans-serif;
 }
</style> 
</head> 
<body> 
<h1> Welcome to nginx!</h1> 
</body> 
</html> 
 

4、运行 nginx

docker run -p 80:80 --name mynginx -v /root/nginx/www:/etc/nginx/www -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/logs:/var/log/nginx -d nginx

a6113e26b00feb8ca084c919316c61da59fad5300b3b5bac5895d09ee86944f2

Docker run 命令的参数说明

  • p 80:80:将容器的80端口映射到主机的80端口
  • --name mynginx:将容器命名为mynginx
  • -v /root/nginx/www:/etc/nginx/www :将主机中当前目录下的www挂载到容器的/etc/nginx/www
  • -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf
  • -v /root/nginx/logs:/var/log/nginx:将主机中当前目录下的logs挂载到容器的/var/log/nginx

5、直接浏览器访问:我的地址:url

6、查看日志

more /root/nginx/logs/access.log

192.168.199.1 - - [25/Mar/2019:15:32:51 +0000] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
192.168.199.1 - - [25/Mar/2019:15:34:28 +0000] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
192.168.199.1 - - [25/Mar/2019:15:34:29 +0000] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
 

7、扩展进入安装的nginx 容器

sudo docker exec -it mynginx /bin/bash


希望我的分享可以帮助到你,如果你在内容技术上遇到难题,可以+关注■@主引教程 ,反馈给我们。我们会及时回复,如果有那些内容有误可以直接提出来,我们会及时纠正,谢谢来访。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表