Docker学习笔记(二)- Nginx的配置
这里我使用的是Hyper-V虚拟机,安装的Ubuntu_14.04系统,先是尝试在ubuntu中完成部署工作。
一、安装Nginx,安装过程参考Docker中的其他文章。
二、将Web文件拷贝到 /usr/local/nginx/html/ 下。
三、配置Nginx的配置文件 vi /usr/local/nginx/conf/nginx.conf
内容如下:
1 #user和pid应该按默认设置 – 我们不会更改这些内容,因为更改与否没有什么不同。 2 #user www www; 3 #nginx对外提供web服务时的worder进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。 4 worker_processes auto; 5 6 #告诉nginx只能记录严重的错误。 7 error_log /usr/local/nginx/logs/error.log crit; 8 #error_log logs/error.log notice; 9 #error_log logs/error.log info; 10 11 pid /usr/local/nginx/logs/nginx.pid; 12 #更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,所以把这个值设高,这样nginx就不会有“too many open files”问题了。 13 worker_rlimit_nofile 1000000; 14 15 events { 16 #设置可由一个worker进程同时打开的最大连接数。 17 worker_connections 65536; 18 #告诉nginx收到一个新连接通知后接受尽可能多的连接。 19 multi_accept on; 20 #设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。 21 use epoll; 22 } 23 24 http { 25 include mime.types; 26 default_type application/octet-stream; 27 28 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 29 '$status $body_bytes_sent "$http_referer" ' 30 '"$http_user_agent" "$http_x_forwarded_for"'; 31 #设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快 32 access_log off; 33 34 #可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效 35 sendfile on; 36 #告诉nginx在一个数据包里发送所有头文件 37 tcp_nopush on; 38 #告诉nginx不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。 39 tcp_nodelay on; 40 # 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。 41 server_tokens off; 42 43 #给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。 44 keepalive_timeout 10; 45 # 设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。 46 client_header_timeout 10; 47 client_body_timeout 10; 48 #告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。 49 reset_timedout_connection on; 50 #指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。 51 send_timeout 10; 52 53 #设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。 54 limit_conn_zone $binary_remote_addr zone=addr:5m; 55 #为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接。 56 limit_conn addr 100; 57 58 #是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。 59 gzip on; 60 #为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。 61 gzip_disable "msie6" 62 #告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了 63 gzip_static on; 64 #允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。 65 gzip_proxied any; 66 #设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。 67 gzip_min_length 1000; 68 #设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。 69 gzip_comp_level 4; 70 #设置需要压缩的数据格式。 71 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 72 73 #打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。 74 open_file_cache max=100000 inactive=20s; 75 #在open_file_cache中指定检测正确信息的间隔时间。 76 open_file_cache_valid 30s; 77 #定义了open_file_cache中指令参数不活动时间期间里最小的文件数。 78 open_file_cache_min_uses 2; 79 #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。 80 open_file_cache_errors on; 81 82 #只是一个在当前文件中包含另一个文件内容的指令。 83 include /etc/nginx/conf.d/*.conf; 84 include /etc/nginx/sites-enabled/*; 85 86 server { 87 #监听80端口 88 listen 80; 89 #服务器ip 90 server_name 115.29.77.97; 91 92 #设置默认的字符集 93 charset utf-8; 94 95 #access_log /usr/local/nginx/logs/host.access.log main; 96 access_log off; 97 #应用根目录。需要把lib和resource文件夹移动到app文件夹中,否则无法找到其中的文件。 另外root要放到server下,而不是location下。 98 root html/ElecManageSystem/app; 99 location / { 100 #首页 101 index index.html index.htm; 102 } 103 104 #error_page 404 /404.html; 105 106 # redirect server error pages to the static page /50x.html 107 # 108 error_page 500 502 503 504 /50x.html; 109 location = /50x.html { 110 #错误页面根目录 111 root html; 112 } 113 114 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 115 # 116 #location ~ \.js$ { 117 # proxy_pass http://115.29.77.97; 118 #} 119 120 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 121 # 122 #location ~ \.php$ { 123 # root html; 124 # fastcgi_pass 127.0.0.1:9000; 125 # fastcgi_index index.php; 126 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 127 # include fastcgi_params; 128 #} 129 130 # deny access to .htaccess files, if Apache's document root 131 # concurs with nginx's one 132 # 133 #location ~ /\.ht { 134 # deny all; 135 #} 136 } 137 138 139 # another virtual host using mix of IP-, name-, and port-based configuration 140 # 141 #server { 142 # listen 8000; 143 # listen somename:8080; 144 # server_name somename alias another.alias; 145 146 # location / { 147 # root html; 148 # index index.html index.htm; 149 # } 150 #} 151 152 153 # HTTPS server 154 # 155 #server { 156 # listen 443; 157 # server_name localhost; 158 159 # ssl on; 160 # ssl_certificate cert.pem; 161 # ssl_certificate_key cert.key; 162 163 # ssl_session_timeout 5m; 164 165 # ssl_protocols SSLv2 SSLv3 TLSv1; 166 # ssl_ciphers HIGH:!aNULL:!MD5; 167 # ssl_prefer_server_ciphers on; 168 169 # location / { 170 # root html/ElecManageSystem/app; 171 # index index.html index.htm; 172 # } 173 #} 174 }
1 worker_processes auto; 2 pid /usr/local/nginx/logs/nginx.pid; 3 4 error_log /usr/local/nginx/logs/error.log crit; 5 worker_rlimit_nofile 1000000; 6 7 events { 8 worker_connections 65536; 9 multi_accept on; 10 use epoll; 11 } 12 13 http { 14 include mime.types; 15 default_type application/octet-stream; 16 17 sendfile on; 18 tcp_nopush on; 19 tcp_nodelay on; 20 server_tokens off; 21 22 keepalive_timeout 10; 23 client_header_timeout 10; 24 client_body_timeout 10; 25 reset_timedout_connection on; 26 send_timeout 10; 27 28 limit_conn_zone $binary_remote_addr zone=addr:5m; 29 limit_conn addr 100; 30 31 gzip on; 32 gzip_disable "msie6" 33 gzip_static on; 34 gzip_proxied any; 35 gzip_min_length 1000; 36 gzip_comp_level 4; 37 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 38 39 open_file_cache max=100000 inactive=20s; 40 open_file_cache_valid 30s; 41 open_file_cache_min_uses 2; 42 open_file_cache_errors on; 43 44 include /etc/nginx/conf.d/*.conf; 45 include /etc/nginx/sites-enabled/*; 46 47 server { 48 listen 80; 49 server_name 115.29.77.97; 50 charset utf-8; 51 access_log off; 52 53 root html/ElecManageSystem/app; 54 location / { 55 index index.html index.htm; 56 } 57 58 error_page 500 502 503 504 /50x.html; 59 location = /50x.html { 60 root html; 61 } 62 } 63 }
然后重启Nginx /usr/local/nginx/sbin/nginx –s reload
四、修改Web中访问Api的Url,/usr/local/nginx/html/ElecManageSystem/app/common/constants/taurusAuthConstant.js
五、查询虚拟机的IP地址。
ifconfig -a
确保虚拟机连接的是物理网络。
六、推出容器并将容器保存为镜像
exit
#查看容器的ID
docker ps -a
两个参数-容器ID-镜像名称
docker commit ContainerID ImageName
不积跬步,无以至千里;不积小流,无以成江海.

浙公网安备 33010602011771号