爱生活,爱分享


Docker安装Nginx

haiten 2019-11-15 662浏览 0条评论
首页/正文
分享到: / / / /

一、准备相关软件包

wget http://nginx.org/download/nginx-1.17.9.tar.gz

二、编写 Dockerfile 文件

nano Dockerfile
-----------------------------------------------------------------------------------------
FROM hub.c.163.com/public/centos:latest

MAINTAINER yanghaiteng 13702721963@139.com

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV LANG=zh_CN.UTF-8

ENV NGINX_PATH=/opt/nginx

EXPOSE 80 443

RUN rpm --rebuilddb \
&& yum install -y wget \
zlib zlib-devel \
pcre pcre-devel \
gcc gcc-c++ \
openssl openssl-devel \
libevent libevent-devel \
perl make tar net-tools \
&& mkdir -p ${NGINX_PATH}

ADD nginx-1.17.9.tar.gz ${NGINX_PATH}/

WORKDIR ${NGINX_PATH}/nginx-1.17.9
RUN ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module \
&& make \
&& make install

ENV PATH /usr/local/nginx/sbin:$PATH

CMD ["nginx", "-g", "daemon off;"]
-----------------------------------------------------------------------------------------

三、执行 Dockerfile 制作镜像

docker build -t yht/centos/nginx:1.17.9 .

四、建立相关文件和编写配置

1、设置目录文件权限

-----------------------------------------------------------------------------------------
mkdir -p /data/frame/nginx
chmod -R 755 /data/frame/nginx
setfacl -d -m o::rx /data/frame/nginx
getfacl /data/frame/nginx
mkdir -p /data/frame/nginx/conf.d  /data/frame/nginx/html /data/frame/nginx/logs
-----------------------------------------------------------------------------------------

2、建立根配置 nginx.conf

nano /data/frame/nginx/nginx.conf
-----------------------------------------------------------------------------------------
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  10240;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    add_header X-Frame-Options SAMEORIGIN;
 
    #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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
 
    include /usr/local/nginx/conf.d/*.conf;
}
-----------------------------------------------------------------------------------------

3、建立子配置节点 conf.d/*.conf

nano /data/frame/nginx/conf.d/nginx.vh.default.conf
-----------------------------------------------------------------------------------------
upstream module-test {
    server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
}

server {
    listen       80;
    server_name  localhost;
    server_name_in_redirect off;
    client_max_body_size 1024M;

    gzip on;
    gzip_comp_level 1;
    gzip_min_length 100;
    gzip_types application/javascript text/css text/xml;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    #charset koi8-r;
    #access_log  logs/host.access.log  main;
 
    location / {
        root  html;
        index  index.html index.htm;
    }

    location ^~ /service-test/ {
        proxy_pass http://module-test/service-test/;
        proxy_set_header   Host   $host;
        proxy_set_header   Referer $http_referer;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   Cookie $http_cookie;
    }
 
    #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   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;
    #}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.crt;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
#    ssl_prefer_server_ciphers on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
-----------------------------------------------------------------------------------------

4、建立测试主页 index.html

nano /data/frame/nginx/html/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>
<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>
-----------------------------------------------------------------------------------------

5、建立测试错误返回页 50x.html

nano /data/frame/nginx/html/50x.html
-----------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
    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>
-----------------------------------------------------------------------------------------

五、使用镜像

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
firewall-cmd --list-all

docker run -d \
--restart always \
--name frame-nginx \
-p 80:80 \
-p 443:443 \
-v /data/frame/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf \
-v /data/frame/nginx/conf.d:/usr/local/nginx/conf.d \
-v /data/frame/nginx/html:/usr/local/nginx/html \
-v /data/frame/nginx/logs:/usr/local/nginx/logs \
yht/centos/nginx:1.17.9

六、测试

浏览器打开:
http://服务器IP

原创不易,如需转载,请标明出处!

最后修改:2019-11-15 07:01:11 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

说点什么吧~

评论列表

还没有人评论哦~赶快抢占沙发吧~