在docker中:docker启动nginx脚本+优化项 ?
docker启动nginx脚本+优化项
1、先启动
docker run -d --name ipu_openresty_14 --restart=always --network=host -p 80:80 -p 443:443 openresty/openresty:latest
2、拷贝容器内的配置文件
1、nginx优化项主配置文件目录
2、业务应用配置(添加虚拟主机使用的)
docker cp ca8514478804:/usr/local/openresty/nginx/conf /ipu-data/openresty/conf
docker cp ca8514478804:/etc/nginx/conf.d /ipu-data/openresty/conf/conf.d
3、修改nginx.conf主配置文件
##### 添加include。(扩展包含虚拟主机)
##### 和nginx优化项配置。
#在修改为auto、
vim /ipu-data/openresty/conf/nginx.conf
worker_processes auto; #启用所有线程
http {
server {
......省略......
}
server {
listen 80;
server_name 127.0.0.1;
location = /test {
default_type text/html;
return 200 'good';
}
}
#下面这里是在HTTP模块添加引用、业务服务的代理配置文件(添加多个虚拟主机)
include /etc/nginx/conf.d/*.conf;
}
4、添加一个测试页面
vim jigaobo-test.conf
server {
listen 80;
server_name 0.0.0.0;
# 指定根目录(为什么要指定这个目录、一会儿启动nginx时候挂载会使用宿主机的这个目录下的静态文件)
root /ipu-data/data_openresty;
# 当用户访问 /test 时,返回 index.html 文件的内容
location = /test {
try_files /index.html =404;
}
# 其他路径的默认处理
location / {
index index.html;
}
}
5、nginx启动命令
#docker部署应用服务单机使用host模式网络、性能最强、少经过一层网络转发。
docker run -d --name ipu_openresty_14 --restart=always --network=host -p 80:80 -p 443:443 -v /ipu-data/openresty/conf:/usr/local/openresty/nginx/conf \
-v /ipu-data/openresty/logs:/usr/local/openresty/nginx/logs \
-v /ipu-data/data_openresty/:/ipu-data/data_openresty \
-v /ipu-data/openresty/conf.d:/etc/nginx/conf.d/ \
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime openresty/openresty:latest
#挂载点解释:
1、挂载nginx自身配置文件目录(优化项配置文件)
2、nginx服务的日志文件挂载点
3、业务数据(前端静态文件html、png)
4、业务服务的代理配置文件(添加多个虚拟主机)
5、挂载宿主机的时间、使容器内时间和宿主机一样
6、nginx优化文件如何优化
看个人生产按需配置、如下可以生产环境使用。
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
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;
tcp_nodelay on;
keepalive_timeout 180;
types_hash_max_size 4096;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types text/plain text/javascript application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
7、修改后重新加载命令
docker exec -it ipu_openresty_14 nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
docker exec -it ipu_openresty_14 nginx -s reload

浙公网安备 33010602011771号