Docker全系列 - Docker部署nginx

302 阅读1分钟

Docker部署nginx

(1):完成Docker环境安装

(2):拉取nginx镜像

[root@localhost ~] docker pull nginx

(3):创建容器

[root@localhost ~] docker run --name [容器名称] -p [宿主机映射端口]:80 -v [项目目录]:/usr/share/nginx/html --link=[容器名称或ID]:[连接别名] -d nginx

# --link仅需在需要解析python,php文件等非静态文件时才需要添加,用于解释文件执行

(4):修改nginx配置

[root@localhost ~] docker exec -it [容器ID] /bin/bash
[root@localhost ~] apt-get update && apt-get install  lsof vim

# nginx.conf 实例参考
user nginx;

worker_processes auto;
worker_cpu_affinity auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
        use epoll;
        multi_accept off;
        accept_mutex off;
        worker_connections  65535;
        #worker_rlimit_nofile 65535;
}
    
    
http {
    include       mime.types;
    default_type  application/octet-stream;

    underscores_in_headers on;
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Headers * always;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS always;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile on;
    sendfile_max_chunk 512k;
    tcp_nopush on;

    keepalive_timeout 60;

    tcp_nodelay on;
    
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";

    #limit_conn_zone $binary_remote_addr zone=perip:10m;
    ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
    
    server_tokens off;
    access_log off;
    include /etc/nginx/conf.d/*.conf;
}

(5):修改虚拟主机配置

# 示例参考
server
{
        listen 80;
        listen  [::]:80;

        server_name home.localhost.com;

        location / {
                if (!-e $request_filename) {
                        rewrite  ^(.*)$  /index.php?s=/$1  last;
                }
                #try_files $uri $uri/ /index.php?$query_string;
                root   /usr/share/nginx/html/gamehall-home/public/;
                index  index.html index.htm index.php;
        }

        location ~ \.php$ {
                root           /var/www/html/gamehall-home/public/;
                fastcgi_pass   [这里换成link连接别名]:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location ~ ^(.*)\/\.git\/ {
                deny all;
        }
}