您好,登錄后才能下訂單哦!
小編給大家分享一下Docker中Nginx環境如何搭建,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Linux 環境
安裝編譯工具及庫文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
首先要安裝 PCRE
PCRE 作用是讓 Nginx 支持 Rewrite 功能
若提示無wget則安裝(在 Docker 拉取的源 centos 中默認是沒安裝的)
yum -y install wget
進入程序安裝目錄
cd /usr/local/src/
下載pcre安裝包
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
解壓安裝包:
tar -zxvf pcre-8.35.tar.gz
刪除安裝包
rm -rf pcre-8.35.tar.gz
進入安裝包
cd pcre-8.35
編譯安裝
./configure make && make install
查看pcre版本
preconv --version
[root@de790c02f7b6 pcre-8.35]# preconv --version GNU preconv (groff) version 1.22.2 with iconv support
安裝 Nginx
下載安裝包
wget http://nginx.org/download/nginx-1.6.2.tar.gz
安裝
cd nginx-1.8.0 ./configure --prefix=/usr/local/nginx make make install
檢查安裝結果
/usr/local/nginx/sbin/nginx -v
腳本
FROM centos:latest ## 制作者信息 MAINTAINER xiachaoyang xiazhaoyang@live.com RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel ## 創建目錄 RUN mkdir -p /usr/local/nginx ## 本地安裝 直接自動解壓 ## ADD nginx-1.8.0.tar.gz /usr/local/src ## 在線安裝 RUN cd /usr/local/src ADD http://nginx.org/download/nginx-1.8.0.tar.gz . RUN tar -zxvf nginx-1.8.0.tar.gz RUN ln -s /usr/local/src /componets RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install RUN rm -vf /usr/local/nginx/conf/nginx.conf ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf EXPOSE 80 # 設置啟動時執行命令 #ENTRYPOINT /usr/local/nginx/sbin/nginx -v
鏡像
docker build -t env-nginx .
docker images
docker tag [鏡像ID] env-nginx:1.0
docker run -i -t -p 80:80 env-nginx:1.0
清空 Nginx Docker
docker rm -f docker container ls -a -q
docker rmi -f env-nginx:2.0
docker build -t env-nginx:2.0 .
docker run -i -t -p 8001:80 —name nginx —restart=always env-nginx:2.0
運行后執行nginx
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -i -t -p 8001:80 --restart=always 127.0.0.1:5000/env-nginx:1.0 [root@56dfba762876 /]# cd usr/local/nginx/sbin/ [root@56dfba762876 sbin]# ls nginx [root@56dfba762876 sbin]# ./nginx -v nginx version: nginx/1.8.0 [root@56dfba762876 sbin]# ps -ef|grep nginx root 18 1 0 07:44 pts/0 00:00:00 grep --color=auto nginx [root@56dfba762876 sbin]# ./nginx [root@56dfba762876 sbin]# ps -ef|grep nginx root 20 1 0 07:45 ? 00:00:00 nginx: master process ./nginx nobody 21 20 0 07:45 ? 00:00:00 nginx: worker process nobody 22 20 0 07:45 ? 00:00:00 nginx: worker process
配置路由
關閉nginx
./nginx -s stop
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56dfba762876 127.0.0.1:5000/env-nginx:1.0 "/bin/bash" 10 minutes ago Up 10 minutes 0.0.0.0:8001->80/tcp brave_kowalevski d75f05b4d7f8 hyper/docker-registry-web "start.sh" 5 days ago Up About an hour 0.0.0.0:8000->8080/tcp local-docker-res-web 1b5239bc943b registry "/entrypoint.sh /etc…" 5 days ago Up About an hour 0.0.0.0:5000->5000/tcp local-docker-res xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker attach 56dfba762876 [root@56dfba762876 sbin]# ls nginx [root@56dfba762876 sbin]# cd .. [root@56dfba762876 nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@56dfba762876 nginx]# cd c client_body_temp/ conf/ [root@56dfba762876 nginx]# cd conf/ [root@56dfba762876 conf]# ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@56dfba762876 conf]# cd ../sbin/ [root@56dfba762876 sbin]# ./nginx -s stop [root@56dfba762876 sbin]# ps -ef|grep nginx root 30 1 0 07:55 pts/0 00:00:00 grep --color=auto nginx
修改路由配置
vi nginx.config
[root@56dfba762876 conf]# cd ../sbin/ [root@56dfba762876 sbin]# ./nginx [root@56dfba762876 sbin]# cd .. [root@56dfba762876 nginx]# cd conf/ [root@56dfba762876 conf]# vi nginx.conf [root@56dfba762876 conf]# cd ../sbin/ [root@56dfba762876 sbin]# ./nginx -s stop [root@56dfba762876 sbin]# ./nginx [root@56dfba762876 sbin]# server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } location /docker-registry-web/ { proxy_pass http://192.168.2.102:8000/; proxy_redirect default; } location /jenkins/ { proxy_pass http://192.168.2.102:8002/; proxy_redirect default; } }
jenkins通過nginx映射效果
以上是“Docker中Nginx環境如何搭建”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。