nginx的配置文件,並在docker下部署nginx


1、docker下部署nginx

#拉取nginx鏡像
docker pull nginx
#執行命令
docker run -e TZ="Asia/Shanghai" -d -p 80:80
      --name amaizi-nginx-web
      -v /opt/project/amaizi/nginx/html:/etc/nginx/static
      -v /opt/project/amaizi/nginx/nginx.conf:/etc/nginx/nginx.conf
      -v /opt/project/amaizi/nginx/logs:/var/log/nginx nginx

注:

  • -e TZ="Asia/Shanghai":標記本地時區,消除docker時區差8個小時問題
  • -d:后台運行
  • -p 80:80 :映射端口為本地80,其中前面端口為宿主機端口
  • --name amaizi-nginx-web :定義容器名稱
  • -v /opt/project/amaizi/nginx/html:/etc/nginx/static :映射/etc/nginx/static到宿主機的/opt/project/amaizi/nginx/html文件夾
  • -v /opt/project/amaizi/nginx/nginx.conf:/etc/nginx/nginx.conf:映射nginx.conf到本地使用的配置文件
  • -v /opt/project/amaizi/nginx/logs:/var/log/nginx nginx:映射日志文件記錄在宿主機中

2、nginx本地配置

# user 指定運行 nginx 的用戶和組(第一個參數為用戶第二個為組,這里只有用戶)
#user  nobody;
# 指定工作進程數(一般設置為CPU核數)
worker_processes  1;   

# 指定錯誤日志為 logs/ 目錄下的 error.log 文件
#error_log  logs/error.log;
# 指定錯誤日志,並指定寫入格式為 notice
#error_log  logs/error.log  notice;
# 指定錯誤日志,並指定寫入格式為 info  
#error_log  logs/error.log  info;

# 指定 pid 文件(存放主進程 pid 號)
#pid        logs/nginx.pid;

# nginx 連接配置模塊
events {
    # 指定每個工作進程最大連接數為 1024
    worker_connections  1024;
}

# http 配置模塊
http {
    # 通過 include 加載 mime.types 文件,里面的 types {} 模塊將文件擴展名映射到響應的 MIME 類型
    include       mime.types;
    # 定義響應的默認 MIME 類型
    default_type  application/octet-stream;

    # 寫入格式 main 的內容格式如下
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    # 指定訪問日志和寫入格式為 main
    #access_log  logs/access.log  main;

    # 啟用或者禁用 sendfile()
    sendfile        on;
    # 啟用或者禁用使用套接字選項(僅在 sendfile 使用時使用)
    #tcp_nopush     on;

    # 0 值禁用保持活動的客戶端連接
    #keepalive_timeout  0;
    # 65 s 超時
    keepalive_timeout  65;

    # 啟用或者禁用 gzip
    #gzip  on;

    # 虛擬主機配置模塊
    server {
        # 監聽 80 端口
        listen       80;
        # 監聽域名為 localhost
        server_name  localhost;
        # root 訪問文件的存儲位置
        root /etc/nginx/static;
        # 將指定的 charset 添加到 “Content-Type” 響應頭字段。如果此charset與source_charset指令中指定的charset不同,則執行轉換。
        #charset koi8-r;

        # 指定該虛擬主機的訪問日志
        #access_log  logs/host.access.log  main;

        # 將特定的文件或目錄重新定位,如 php 文件,image 目錄等
        location / {
            # 設置請求的根目錄
            root   /etc/nginx/static;
            # 定義索引,按順序匹配
            index  index.html index.htm;
        }

        # 定義顯示 404 錯誤的 uri
        #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'
        location = /50x.html {
            root   /etc/nginx/static;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        # 正則表達式匹配 php 文件
        #location ~ \.php$ {
            # 設置代理服務器的協議和地址,以及應該映射位置的可選URI。作為協議,可以指定“http”或“https”。該地址可以指定為一個域名或IP地址,以及一個可選端口
        #    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 服務器的地址。地址可以指定為一個域名或 IP 地址,以及一個端口
        #    fastcgi_pass   127.0.0.1:9000;
             # 設置將在以斜杠結尾的URI之后追加的文件名,
        #    fastcgi_index  index.php;
             # 設置一個應該傳遞給FastCGI服務器的參數。
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
             # 加載 conf/fastcgi_params 文件
        #    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
    #
    # ssl 配置,要啟用 ssl 模塊需要在編譯 nginx 時加上 --with-http_ssl_module 參數
    #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;
    #    }
    #}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM