docker 安装nginx 配置udp
目标:配置 nginx 配置udp 反向代理
操作系统:windows 10
一、创建docker
第一步,创建Dockerfile
[code]# 选择一个已有的os镜像作为基础 FROM centos:6.7 # 镜像的作者 MAINTAINER Fisher "skzstory@163.com" # 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no RUN yum install -y openssh-server sudo RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config # 添加用户skz,密码123456 RUN /bin/echo 'root:123456' |chpasswd RUN useradd skz RUN /bin/echo 'skz:123456' |chpasswd RUN useradd -s /sbin/nologin -M nginx RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local # 下面这两句比较特殊,在centos6上必须要有,否则创建出来的容器sshd不能登录 RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key # 启动sshd服务并且暴露22端口 CMD /usr/sbin/sshd -D EXPOSE 22 EXPOSE 80 EXPOSE 53
第二步,创建Docker镜像
docker build -t skz/centos:6.7 .
二、创建并运行新容器
运行容器
第一步
docker run -t -i -p 53 skz/centos:6.7 /bin/bash
第二步,查看容器
docker ps
[code]D:\tst\docker>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b42ac71923f9 skz/centos:6.7 "/bin/sh -c '/usr/..." 13 hours ago Up 13 hours 0.0.0.0:32769->22/tcp, 0.0.0.0:32768->80/tcp mytest 234c00c61f4a webcenter/activemq:latest "/app/run.sh" 15 months ago Up 14 hours 1883/tcp, 5672/tcp, 0.0.0.0:8161->8161/tcp, 61613-61614/tcp, 0.0.0.0:61616->61616/tcp activemq
注:查看其他相关配置情况:
docker inspect mytest
三、安装nginx
第一步、在容器mytest中开启一个交互模式的终端
docker exec -ti mytest /bin/bash
第二步,安装相关软件
yum -y install gcc-c++
yum -y install proc* openssl* pcre*
yum -y install wget
yum -y install tar
yum -y install vim
# 网络工具netcat
yum install -y nc
第三步,安装nginx
$ cd /opt
$ wget http://nginx.org/download/nginx-1.15.3.tar.gz
$ tar zxvf nginx-1.15.3.tar.gz
$ cd nginx-1.15.3
配置编译参数:
$ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_sub_module --with-stream
$ make
$ make install
第四步,启动nginx
whereis nginx
[code][root@b42ac71923f9 nginx-1.15.3]# whereis nginx nginx: /usr/local/nginx
cd /usr/local/nginx/sbin
./nginx
第五步,查看nginx启动情况
ps -ef|grep nginx
[code][root@b42ac71923f9 sbin]# ps -ef|grep nginx root 10223 0 0 05:06 ? 00:00:00 nginx: master process ./nginx nginx 10224 10223 0 05:06 ? 00:00:00 nginx: worker process root 10226 127 0 05:08 pts/0 00:00:00 grep nginx
可以浏览器查看80端口网页,显示如下
三、配置 stream模块
$ cd /usr/local/nginx/conf
$ vim nginx.conf
增加
[code]stream { upstream udp_proxy { server 220.26.106.221:11011; } server { listen 192.168.10.195:11899 udp; proxy_responses 1; proxy_timeout 20s; proxy_pass udp_proxy; error_log logs/dns.log; } }
四、测试
测试环境:
第一步,搭建一个udp测试服务
udp服务器上,新建udpserver.c
[code]#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<string.h> #include<arpa/inet.h> #include<netinet/in.h> #include<unistd.h> //打印输入提示选项 static void* usage(const char* port) { printf("usage: %s [local_ip] [local_port]\n",port); } int main(int argc,char* argv[]) { if(argc!=3) { usage(argv[0]); return 1; } //创建套接字 int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { perror("socket"); exit(1); } //将套接字与ip地址和端口号进行绑定 struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(atoi(argv[2])); local.sin_addr.s_addr = inet_addr(argv[1]); if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0) { perror("bind"); exit(2); } char buf[1024]; struct sockaddr_in client; socklen_t len = sizeof(client); char* msg = "Have a goog day"; while(1) { //读取数据 int r = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len); if(r<0) { perror("recvfrom"); exit(3); } else { buf[r] = 0; printf("[%s : %d]# %s\n",inet_ntoa(client.sin_addr), ntohs(client.sin_port),buf); //回送数据 if(sendto(sock,msg,strlen(msg),0,(struct sockaddr*)&client,len)<0) { perror("sendto"); exit(4); } break; } } return 0; }
编译
[code]gcc udpserver.c -o udpserver
启动udp测试服务
[code]./udpserver 220.26.106.221 11011
第二步,使用ncat进行udp测试
工作站pc测试
[code][root@05fe2f6f9a0d nginx]# nc -vuz 220.26.106.221 11011 Connection to 127.0.0.1 53 port [udp/domain] succeeded! [root@05fe2f6f9a0d nginx]# nc -vuz 192.168.10.195 11899 Connection to 172.17.0.3 53 port [udp/domain] succeeded!
五、保存镜像
[code]C:\Users\Administrator>docker commit -a "skz" -m "nginx1.15.3 with centos6.7 and ssh" 71acec45581c skz/centos6.7/nginx1.15.3:v1 sha256:9d905a71d0bc27f17fcd6820987e4d7e5f4db9f5ba557f2e8196e87a12b5fc22
相关工具:
查看nginx是否启动,以及安装地址
[code]ps -ef|grep nginx
检查nginx.conf是否正确
./nginx -t
[code][root@b42ac71923f9 sbin]# ./nginx -t nginx: [emerg] unknown directive "stream" in /usr/local/nginx/conf/nginx.conf:17 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
查nginx版本
./nginx -v
重启nginx
./nginx -s reload
备份nginx.conf文件
mv nginx.conf{,.bak}
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
关闭nginx:
nginx -s stop :快速停止nginx
quit :完整有序的停止nginx
kill nginx进程
pkill -9 nginx
查找nginx目录及文件
find / -name nginx*
删除nginx
sudo rm -r /usr/local/nginx
查看端口
netstat -tunlp |grep 8000
安装网络工具
yum install net-tools
根据端口号查进程
netstat -nap | grep 端口号
阅读更多- centos使用docker下安装mysql并配置、nginx
- Docker 安装 Nginx 并个性化挂载配置文件 nginx.conf
- CentOS安装和配置Docker和Nginx
- nginx 安装与配置
- Nginx安装配置与服务搭建
- Ubuntu/CentOS 系统上安装与配置Nginx
- 在ubuntu上安装和配置nginx https站点
- CentOS安装和配置Nginx
- centos7使用docker下安装mysql并配置
- ubuntu_14.04.2安装配置laravel+nginx
- Nginx安装及配置详解
- CentOS 6.7下配置 yum 安装 Nginx。
- Ubuntu中Nginx的安装与配置
- Ubuntu/CentOS 系统上安装与配置Nginx
- mac 安装nginx 配置识别php文件
- docker上安装nginx服务
- nginx安装及简要配置
- Ubuntu环境下安装配置Nginx
- nginx 安装、配置、参数介绍
- Cubieboard A10 安装Nand系统,配置nginx,php,mysql,samba详细教程