docker 运行jenkins及vue项目与springboot项目,四.docker运行nginx

docker 运行jenkins及vue项目与springboot项目:

准备配置

创建 /home/docker/nginx/nginx.conf 文件及/home/docker/nginx/log文件夹

其nginx.conf 文件为在原nginx.conf上添加上自定义的一些配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #
     upstream blog {
        server localhost:8089;
        #hash $remote_addr;
        #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        #check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
        # check_http_expect_alive http_2xx http_3xx;
        #                                #keepalive 60;
        #                                        ip_hash;
        }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/web/www;
            try_files $uri $uri/ /index.html last;
            index  index.html index.htm;
           # error_page 405 =200 http://$host$request_uri;
        }

        location /blog {
                proxy_pass http://blog;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
运行命令

若没有镜像则会自动pull镜像

docker run -p 8080:80 -v /home/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/docker/nginx/log:/var/log/nginx -v /home/docker/jenkins/jenkins_home/www:/var/web/www -d docker.io/nginx

其中:

-p 8080:80 将8080端口映射至docker的80端口

-v /home/docker/nginx/nginx.conf:/etc/nginx/nginx.conf 配置映射:服务器中的nginx配置映射至容器的nginx配置中

-v /home/docker/nginx/log:/var/log/nginx 日志映射

-v /home/docker/jenkins/jenkins_home/www:/var/web/www 网页文件映射:jenkins打包后的web页面文件夹映射至nginx配置的web文件夹(在配置文件中注意更改location /{root /var/web/www; ...})

访问http://192.168.37.136:8080即可看到jenkins打包的git项目后的页面
若出现404 可能为网络不通 其前端nginx访问后端时在docker 中

解决:将nginx.conf中的配置的service地址更换为docker 地址,而非localhost

# 。。。
upstream blog {
        server {docker的ip}:8089;
        #hash $remote_addr;
        #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        #check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
        # check_http_expect_alive http_2xx http_3xx;
        #                                #keepalive 60;
        #                                        ip_hash;
        }
# 。。。