首先拉取nginx镜像

  1. ##搜索镜像
  2. docker search nginx
  3. ##拉取镜像
  4. docker pull nginx

创建nginx容器并启动

  1. docker run ##docker 命令
  2. --name nginx-81 ##设置容器名称
  3. -p 81:80 -p 443:443 ## 设置容器端口与服务器端口映射关系,语法:-p 主机端口:容器对外端口
  4. -v /root/Documents/config/nginx/cert:/etc/nginx/cert ##建立软连接
  5. -v /root/Documents/config/nginx/nginx.conf:/etc/nginx/nginx.conf
  6. -v /root/Documents/config/nginx/html:/usr/share/nginx/html
  7. -v /root/Documents/config/nginx/logs:/var/log/nginx
  8. -d nginx ## 定义后台运行 镜像名称

copy内容

  1. docker run
  2. --name nginx-80 \
  3. -p 80:80 -p 443:443 \
  4. -v /root/Documents/config/nginx/cert:/etc/nginx/cert \
  5. -v /root/Documents/config/nginx/conf.d:/etc/nginx/conf.d:ro \
  6. -v /root/Documents/config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  7. -v /root/Documents/config/nginx/html:/usr/share/nginx/html \
  8. -v /root/Documents/config/nginx/logs:/var/log/nginx \
  9. --restart=always \
  10. --neywork=webnet \
  11. -d nginx

这里需要在/root/Documents/config/nginx/目录想创建配置文件,建立软连接,这个方便维护操作,直接修改此处的配置文件,restart容器即可生效。

目录结构

image.png
cert/目录下为网站SSL密钥文件

nginx/conf.d/default.conf

  1. upstream leaf{
  2. server www.baidu.com;
  3. }
  4. server {
  5. listen 80;
  6. listen [::]:80;
  7. server_name javaleaf.cn www.javaleaf.cn;
  8. location ^~ /.well-known/acme-challenge/ {
  9. default_type "text/plain";
  10. root /usr/share/nginx/html;
  11. }
  12. location = /.well-known/acme-challenge/ {
  13. return 404;
  14. }
  15. location / {
  16. proxy_pass http://www.163.com;
  17. proxy_redirect off;
  18. }
  19. error_page 500 502 503 504 /50x.html;
  20. location = /50x.html {
  21. root html;
  22. }
  23. }
  24. server {
  25. listen 443 ssl;
  26. listen [::]:443 ssl;
  27. server_name javaleaf.cn;
  28. #ssl on;
  29. ssl_prefer_server_ciphers on;
  30. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  31. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  32. ssl_session_cache shared:SSL:10m;
  33. ssl_session_timeout 10m;
  34. ssl_certificate /etc/nginx/cert/4265089_javaleaf.cn.pem;
  35. ssl_certificate_key /etc/nginx/cert/4265089_javaleaf.cn.key;
  36. location / {
  37. proxy_redirect off;
  38. proxy_pass http://www.163.com;
  39. }
  40. location ^~ /.well-known/acme-challenge/ {
  41. default_type "text/plain";
  42. root /usr/share/nginx/html;
  43. }
  44. location = /.well-known/acme-challenge/ {
  45. return 404;
  46. }
  47. }
  48. server {
  49. listen 443 ssl;
  50. listen [::]:443 ssl;
  51. server_name www.javaleaf.cn;
  52. #ssl on;
  53. ssl_prefer_server_ciphers on;
  54. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  55. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  56. ssl_session_cache shared:SSL:10m;
  57. ssl_session_timeout 10m;
  58. ssl_certificate /etc/nginx/cert/4265089_javaleaf.cn.pem;
  59. ssl_certificate_key /etc/nginx/cert/4265089_javaleaf.cn.key;
  60. location / {
  61. proxy_redirect off;
  62. proxy_pass http://www.163.com;
  63. }
  64. location ^~ /.well-known/acme-challenge/ {
  65. default_type "text/plain";
  66. root /usr/share/nginx/html;
  67. }
  68. location = /.well-known/acme-challenge/ {
  69. return 404;
  70. }
  71. }

nginx.conf

  1. #user nobody;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. client_max_body_size 10M;
  14. include /etc/nginx/conf.d/*.conf;
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"'; #自定义日志格式
  18. access_log /var/log/nginx/access.log main;
  19. }

这里暂时还没有进行负载均衡配置,因为一配置就报502错误,暂未清楚原因。

暂未实验:
–net host :启动模式 建议使用 否则做负载均衡会有问题!