【QA】源代码安装nginx和docker镜像创建nginx的区别

本文概述

在Linux上部署nginx,有两种方法:

  • 直接在Linux本机上安装nginx(源代码编译安装)
  • 使用docker镜像,容器化部署nginx

这两种方法安装的nginx,在配置文件的内容上有一些区别,本文主要概述这些差异。

方式1:nginx源码安装

这一章介绍的是通过压缩包编译、安装所部署的nginx的配置文件结构,安装过程不再赘述

nginx版本:nginx-1.18.0.tar.gz

目录结构

.
├── client_body_temp
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf					# 核心配置文件
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp
├── html							# 放置静态资源
│   ├── 50x.html
│   └── index.html
├── logs							# 放置日志文件
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── proxy_temp
├── sbin
│   └── nginx
├── scgi_temp
└── uwsgi_temp

分析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  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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.pem;
    #    ssl_certificate_key  cert.key;

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

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

可以发现:

  • 默认80端口的定义和配置就在该文件里面
  • 通过注释的形式,给出了扩展server的写法

怎样增加自己的配置

用户想要部署自己的项目,就在这个配置文件中写就可以了,新增server配置即可

方式2:docker容器部署

这一章介绍的是通过docker容器部署的nginx的配置文件结构,安装过程不再赘述

nginx版本:nginx:latest

目录结构

docker容器默认将nginx安装在:/etc/nginx/路径下

/etc/nginx/
├── conf.d
│   ├── default.conf
├── fastcgi_params
├── mime.types
├── modules
├── nginx.conf				# 核心配置文件
├── scgi_params
├── uwsgi_params

分析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;
}

可以发现:

  • 对比通过源代码形式安装的nginx配置文件,内容短了很多
  • 日志定义的位置,不在nginx安装的目录下面,而是到了var/log/nginx/路径下
  • 没有在该文件里面找到关于默认80端口的定义,但是文件末尾多了一行引用:include /etc/nginx/conf.d/*.conf;

根据目录结构,去conf.d文件夹下面,看看default.conf的内容

分析default.conf

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;
    #}
}

可以看到:

  • 该文件里面,出现了关于默认80端口的定义
  • 该文件只有server块,相当于其他部分已经在nginx.conf文件中定义好了

怎样增加自己的配置

分析这一行引用:include /etc/nginx/conf.d/*.conf; 可知:

  • conf.d文件夹下面,所有以.conf结尾的文件都会被引用到nginx.conf主配置文件里面
  • 因此,用户想要新增自己的配置,只需要在conf.d文件夹下面新增一个xxx.conf文件,写入server配置即可
  • 这样的好处就是:所有自己的改动,是在一个新的文件里面,不影响默认文件的内容

docker启动nginx–相对比较全面的命令

目录结构

准备一个路径:/xxx/xxxx

.
├── conf							# 用于放置配置文件
│   ├── conf.d
│   │   ├── default.conf			### 从容器里面copy下来的
│   │   ├── icwp-django.conf		### 自定义的
│   │   └── icwp-vue.conf			### 自定义的
│   └── nginx.conf					### 从容器里面copy下来的
├── html							# 用于放置静态资源
│   ├── 50x.html
│   ├── index.html
├── log								# 用于挂载日志

若是要对默认配置文件做改动,先启动一个容器,将里面需要的配置文件拉出来即可,然后将这个容器删除

# 运行nginx容器
docker run  -it -d --name=test_nginx  -p 80:80 nginx

# 拷贝文件到当前路径下
docker cp test_nginx:/etc/nginx/nginx.conf nginx.conf
docker cp test_nginx:/etc/nginx/conf.d/default.conf default.conf
docker cp test_nginx:/usr/share/nginx/html/index.html index.html
docker cp test_nginx:/usr/share/nginx/html/50x.html 50x.html

# 删除容器
docker rm -f test_nginx

docker启动nginx

docker run -d \
-p 80:80 \
--name nginx-test \
-v ./conf/nginx.conf:/etc/nginx/nginx.conf \		# 挂载主配置文件
-v ./conf/conf.d:/etc/nginx/conf.d \				# 挂载自定义的配置文件
-v ./log:/var/log/nginx \							# 挂载日志文件
-v ./html:/usr/share/nginx/html \					# 挂载静态资源文件
nginx:latest
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值