一、docker下部署nginxphp
#拉取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
注:html
-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:映射日誌文件記錄在宿主機中
二、nginx本地配置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;
# }
#}
}