OASCloud-docker应用测试服务器-nginx
History | |||||
NO. | Version | Contents | Time | Persion | Remarks |
1 | V1.0 | Create documents | 2019-07-01 | Qinfang Ye | |
2 | V1.1 | Add ‘docker安装nginx’ | 2019-07-08 | Qinfang Ye | |
OASCloud-docker应用测试服务器-nginx1
1、 下载安装3
(1) 安装准备3
(2)安装Nginx3
2、 配置5
(1) 修改配置文件5
(2) server配置5
(3) 其他配置5
3、 docker安装nginx6
(1)拉取镜像6
(2)映射配置6
- 下载安装
首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定/usr/local为安装目录,以下具体版本号根据实际改变。
- 安装准备
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
(2)安装Nginx
安装之前,最好检查一下是否已经安装有nginx
$ find -name nginx
如果系统已经安装了nginx,那么就先卸载
$ yum remove nginx
首先进入/usr/local目录
$ cd /usr/local
从官网下载最新版的nginx
$ wget http://nginx.org/download/nginx-1.7.4.tar.gz
解压nginx压缩包
$ tar -zxvf nginx-1.7.4.tar.gz
会产生一个nginx-1.7.4 目录,这时进入nginx-1.7.4目录
$ cd nginx-1.7.4
接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装
$ ./configure $默认安装在/usr/local/nginx
$ make
$ make install
如果没有报错,顺利完成后,最好看一下nginx的安装目录
$ whereis nginx
启动:
/usr/local/nginx/sbin/nginx
检查:
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
部分命令如下:
重启:
$ /usr/local/nginx/sbin/nginx –s reload
停止:
$ /usr/local/nginx/sbin/nginx –s stop
测试配置文件是否正常:
$ /usr/local/nginx/sbin/nginx –t
强制关闭:
$ pkill nginx
- 配置
- 修改配置文件
/usr/local/nginx/conf/nginx.conf - server配置
接收请求的服务器需要将不同的请求按规则转发到不同的后端服务器上,在 nginx 中我们可以通过构建虚拟主机(server)的概念来将这些不同的服务配置隔离。
server {
listen 80; #指定服务1端口
server_name localhost; #指定服务IP
root html;
index index.html index.htm;
}
如修改为:
server {
listen 80;
server_name host2;
root /data/www/html;
index index.html index.htm;
} - 其他配置
Localtion、静态文件映射等等,可以通过apollo进行配置,相关配置根据实际项目进行配置。实际项目用docker安装nginx,并映射配置后,完成配置。
- docker安装nginx
(1)拉取镜像
docker pull nginx
(2)映射配置
#创建文件夹 mkdir -p /home/nginx
#新建文件(已经是经过修改的配置文件): cat > nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 65; types_hash_max_size 2048; 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 default_server; listen [::]:80 default_server; server_name localhost; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/; index index.html; } location /api/web { proxy_pass http://192.168.3.212:9010/web; } location /others { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.3.123:9010/others; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } |
#执行映射命令
docker run -d --name nginx_01 -p 80:80 -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/logs:/var/log/nginx -v /home/nginx/conf.d:/etc/nginx/conf.d -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
#实际项目通过docker执行
docker run --name tengine-web -d -p 9527:80 \
-v /usr/local/tengine/logs:/var/log/nginx \
-v /usr/local/tengine/conf.d:/etc/nginx/conf.d \
-v /usr/local/tengine/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/tengine/html:/usr/share/nginx/html nginx
#端口 80:80解释:前面的80可任意修改,作为访问端口,后面的是nginx默认端口
# -v后面的数据,冒号前面为html本地存放路径,对应配置文件的默认路径
/usr/share/nginx/html、/var/log/nginx、/etc/nginx/conf.d、/etc/nginx/nginx.conf
#前端和resource文件夹都放在/home/nginx/html文件夹里面,然后还需要加配置就在conf.d文件夹里面加.conf文件
#登录访问:192.168.3.212:80
#附配置详解
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; # 运行用户,默认是nginx worker_processes auto; # nginx进程数,一般设置为和cpu核数一样 error_log /var/log/nginx/error.log; # 全局错误日志路径 pid /run/nginx.pid; # 进程pid路径 # Load dynamic modules. See /usr/share/nginx/README.dynamic. # 负载动态模块 include /usr/share/nginx/modules/*.conf; events { # 工作模式与连接数上限 worker_connections 1024; # 单个进程的最大连接数 } http { # 设置http服务器 log_format main '$http_host $server_addr $remote_addr [$time_local] "$request" $status $request_body $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time'; # 设置日志的格式 access_log /var/log/nginx/access.log main; # 访问日志的路径 sendfile on; # 开启高效传输模式 tcp_nopush on; # 激活tcp_nopush参数可以允许把http response header和文件的开始放在一个文件里发布,作用是减少网络报文段的数量 tcp_nodelay on; # 激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能 keepalive_timeout 65; # 长连接超时时间,单位是秒 types_hash_max_size 2048; # 为了快速处理静态数据集,例如服务器名称, 映射指令的值,MIME类型,请求头字符串的名称,nginx使用哈希表 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 default_server; # 监听端口 listen [::]:80 default_server; server_name _; # 域名 root /usr/share/nginx/html; # 站点根目录,即网站程序存放目录 # Load configuration files for the default server block. # 默认服务器块的加载配置文件 include /etc/nginx/default.d/*.conf; location / { # 对“/”启用反向代理 } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } } |
#实际项目配置(测试服务器)
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 65; types_hash_max_size 2048; 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 default_server; listen [::]:80 default_server; server_name localhost; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/PaaS/; try_files $uri /index.html; index index.html; } location /api/ { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.3.206:31101/; proxy_cookie_path / /api; } location /others { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.3.206:31101/others; } location /oas-cloud { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.3.212:30103/oas-cloud; } location /tengine { proxy_pass http://127.0.0.1:8080/; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 8080; server_name localhost; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/tengine/; try_files $uri /index.html; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } |