docker配置nginx
阿新 • • 發佈:2021-01-30
下載nginx的映象就不說了,直接配置容器吧!
建立容器
docker run -d -p 80:80 -p 443:443 --name nginx-yixiecha -v /mydata/nginx/www:/usr/share/nginx/html -v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /mydata/nginx/logs:/var/log/nginx -v /mydata/nginx/cert:/etc/nginx/cert -v /home/upload:/etc/nginx/upload nginx
掛載資料夾:
- /mydata/nginx/www:/usr/share/nginx/html 前端檔案
- /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx的配置檔案
- /mydata/nginx/logs:/var/log/nginx 日誌檔案
- /mydata/nginx/cert:/etc/nginx/cert ssl的相關檔案
- /home/upload:/etc/nginx/upload 靜態檔案
配置檔案
server {
listen 80;
server_name ×××.×××××.×××;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html last;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
}
server{
listen 443 ssl;
server_name ×××.×××××.×××;
#listen [::]:81 default_server ipv6only=on;
#ssl on;
ssl_certificate cert/×××.×××××.×××.pem;
ssl_certificate_key cert/×××.×××××.×××.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 靜態檔案
location /file/ {
alias /etc/nginx/upload/;
}
location / {
proxy_pass http://××.×××.×××.××:8008;
}
access_log /var/log/api.log;
}
# http重定向到https
server {
listen 80;
server_name ×××.×××××.×××;
return 301 https://$host$request_uri;
}
靜態檔案配置的注意事項
alias
server{
listen 7001;
server_name 127.0.0.1;
location /file/ {
alias /home/china/areas/;
}
}
此時,通過瀏覽器訪問http://127.0.0.1:7001/file/t.txt,則訪問伺服器的檔案是/home/china/areas/t.txt
root
server{
listen 7002;
server_name 127.0.0.1;
location /test/ {
root /home/china/areas/;
}
}
此時,通過瀏覽器訪問http://127.0.0.1:7001/test/t.txt,則訪問伺服器的檔案是/home/china/areas/test/t.txt
上述兩種方法均可達到目的,區別是它們對路徑的解析方式不同,alas會把指定路徑當作檔案路徑,而root會把指定路徑拼接到檔案路徑後,再進行訪問。