nginx

  1. [运行镜像]
  2. : docker pull registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
  3. # mkdir /opt/data/nginx/:/usr/local/nginx/
  4. : docker run \
  5. --name nginx-blog \
  6. -p 80:80 -p 443:443 \
  7. -v /opt/data/nginx/:/usr/local/nginx/ \
  8. -d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2 \
  9. ./run.sh
  10. [配置静态网站]
  11. : ./nginx -s reload # 重启 nginx
  12. : cd /opt/data/nginx/ && blog/ # 符合博客目录文件
  13. : /usr/local/nginx/conf/nginx.conf
  14. worker_processes 1;
  15. events {
  16. worker_connections 1024;
  17. }
  18. http {
  19. include mime.types;
  20. default_type application/octet-stream;
  21. sendfile on;
  22. keepalive_timeout 65;
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. location / {
  27. root html;
  28. index index.html index.htm;
  29. }
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. }
  35. }
  36. // 添加服务配置
  37. server {
  38. listen 80;
  39. server_name blog.thinxz.cn;
  40. location / {
  41. root blog;
  42. index index.html index.htm;
  43. }
  44. error_page 500 502 503 504 /50x.html;
  45. location = /50x.html {
  46. root html;
  47. }
  48. }

安装

  1. [1、安装 基本编译工具及库]
  2. : yum install update
  3. : yum install -y \
  4. vim wget curl
  5. : yum install -y \
  6. zlib zlib-devel libtool \
  7. openssl openssl-devel \
  8. gcc-c++ make
  1. [2、安装 PCRE - 作用是让 Nginx 支持 Rewrite 功能]
  2. : wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
  3. : tar zxvf pcre-8.35.tar.gz && cd pcre-8.35
  4. : ./configure && make //: make install # 不执行安装
  5. : pcre-config --version
  6. [3、安装 Nginx]
  7. : wget http://nginx.org/download/nginx-1.6.2.tar.gz
  8. // wget http://nginx.org/download/nginx-1.9.0.tar.gz
  9. : tar zxvf nginx-1.6.2.tar.gz && cd nginx-1.6.2
  10. // 简单编译项目
  11. ./configure --prefix=/opt/nginx/bin \
  12. --with-pcre=/opt/nginx/pcre-8.35 \
  13. --with-http_ssl_module \
  14. --with-stream
  15. # /opt/nginx/pcre-8.35 # 下载编译目录
  16. # /usr/local/nginx # nginx 安装目录, 注意路径, 配置成安装路径
  17. ./configure \
  18. --prefix=/usr/local/nginx \
  19. --with-http_stub_status_module \
  20. --with-http_ssl_module \
  21. --with-pcre=/opt/nginx/pcre-8.35
  22. // --with-http_stub_status_module
  23. --user=www --group=www \
  24. --with-http_gzip_static_module \
  25. --http-client-body-temp-path=/usr/local/nginx/tmp/client/ \
  26. --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ \
  27. --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ \
  28. --with-poll_module \
  29. --with-file-aio \
  30. --with-http_realip_module \
  31. --with-http_addition_module \
  32. --with-http_addition_module \
  33. --with-http_random_index_module --with-http_stub_status_module
  34. --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp
  35. --http-scgi-temp-path=/usr/local/nginx/scgi_temp
  36. : make // make install
  37. : /usr/local/nginx/sbin/nginx -v
  1. [commit]
  2. : docker login --username=thinxz registry.cn-hangzhou.aliyuncs.com
  3. : docker commit -m="ngrok" -a="thinxz" cid registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
  4. : docker push registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
  5. [run]
  6. : docker run \
  7. --name nginx09 \
  8. -p 80:80 -p 443:443 \
  9. -v /opt/data/nginx/:/usr/local/nginx/ \
  10. -d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2 \
  11. ./run.sh
  12. [run.sh]
  13. #!/bin/sh
  14. # run
  15. rm -rf /usr/local/nginx/nginx-run.log
  16. echo y | cp -rf /opt/nginx/ngconf/* /usr/local/nginx/
  17. ./opt/nginx/nginx-1.6.2/objs/nginx -c /usr/local/nginx/conf/nginx.conf 2>&1 | tee /usr/local/nginx/nginx-run.log
  18. # suspend main
  19. read -p "press any key to continue." var

配置

  1. [1、创建Nginx 运行使用的用户]
  2. : /usr/sbin/groupadd thinxz
  3. : /usr/sbin/useradd -g thinxz thinxzpassword
  4. : passwd thinxzpassword
  5. [2、配置nginx.conf ]
  6. : 查看配置文件 cat /usr/local/nginx/conf/nginx.conf
  7. [3、测试配置文件]
  8. : /usr/local/nginx/sbin/nginx -t
  9. [4、启动Nginx]
  10. : /usr/local/nginx/sbin/nginx
  11. : /usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
  12. : /usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
  13. : /usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
  1. [nginx docker run]
  2. docker run \
  3. --name nginx06 \
  4. -p 80:80 -p 81:81 -p 443:443 -p 9876:9876 \
  5. -v /opt/data/nginx/nginx.conf:/etc/nginx/nginx.conf \
  6. -v /opt/data/nginx/logs:/data/logs \
  7. -d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:v2
  8. // -e 环境变量, 配置数据库
  9. : docker run \
  10. --name nginx06 \
  11. -p 80:80 -p 81:81 -p 443:443 -p 9876:9876 \
  12. -v /opt/data/nginx/nginx.conf:/etc/nginx/nginx.conf \
  13. -v /opt/data/nginx/logs:/data/logs \
  14. -e DB_HOST=rm-bp10h4rjh8q877420lo.mysql.rds.aliyuncs.com \
  15. -e DB_USER=nginx \
  16. -e DB_PASS=nginx \
  17. -e DB_PORT=3306 \
  18. -d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:v2
  19. //
  20. docker run \
  21. --name nginx \
  22. -p 80:80 -p 443:443 -p 81:81 \
  23. -e DB_HOST=rm-bp10h4rjh8q877420lo.mysql.rds.aliyuncs.com \
  24. -e DB_USER=nginx \
  25. -e DB_PASS=nginx \
  26. -e DB_PORT=3306 \
  27. -e DB=nginx \
  28. -d registry.cn-hangzhou.aliyuncs.com/youdt/nginx:v2
  29. // 宿主机端口只能映射一个容器端口, 一个容器端口可以被多个宿主机端口映射
  30. // host:80 -> container:80
  31. // host:80 -> container:80 && host:81 -> container:80
  32. // 需求, nginx 容器监听宿主机80端口, 根据域名不同将80端口数据, 转发给不同业务容器
  33. # 方案一
  34. // host:80 -> container-ngxin:80
  35. // nginx 映射配置 -> ngxin:80 -> (host:50001)
  36. // host:50001 -> container-git:80
  37. # 方案二
  38. // host:80 -> container-ngxin:80
  39. // nginx 映射配置 -> ngxin:80 -> container-git-ip:80
  40. # 删除容器
  41. // docker ps -a
  42. // docker container rm container-id
  43. # 删除镜像
  44. // docker images
  45. // docker image rm image-id

nginx-db.sql

nginx-s.sql

nginx.conf - 初始配置

  1. # run nginx in foreground
  2. daemon off;
  3. user root;
  4. # Set number of worker processes automatically based on number of CPU cores.
  5. worker_processes auto;
  6. # Enables the use of JIT for regular expressions to speed-up their processing.
  7. pcre_jit on;
  8. error_log /data/logs/error.log warn;
  9. # Includes files with directives to load dynamic modules.
  10. include /etc/nginx/modules/*.conf;
  11. events {
  12. worker_connections 1024;
  13. }
  14. http {
  15. include /etc/nginx/mime.types;
  16. default_type application/octet-stream;
  17. sendfile on;
  18. server_tokens off;
  19. tcp_nopush on;
  20. tcp_nodelay on;
  21. client_body_temp_path /tmp/nginx/body 1 2;
  22. keepalive_timeout 65;
  23. ssl_prefer_server_ciphers on;
  24. gzip on;
  25. proxy_ignore_client_abort off;
  26. client_max_body_size 2000m;
  27. server_names_hash_bucket_size 64;
  28. proxy_http_version 1.1;
  29. proxy_set_header X-Forwarded-Scheme $scheme;
  30. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31. proxy_set_header Accept-Encoding "";
  32. proxy_cache off;
  33. proxy_cache_path /var/lib/nginx/cache/public levels=1:2 keys_zone=public-cache:30m max_size=192m;
  34. proxy_cache_path /var/lib/nginx/cache/private levels=1:2 keys_zone=private-cache:5m max_size=1024m;
  35. # MISS
  36. # BYPASS
  37. # EXPIRED - expired, request was passed to backend
  38. # UPDATING - expired, stale response was used due to proxy/fastcgi_cache_use_stale updating
  39. # STALE - expired, stale response was used due to proxy/fastcgi_cache_use_stale
  40. # HIT
  41. # - (dash) - request never reached to upstream module. Most likely it was processed at Nginx-level only (e.g. forbidden, redirects, etc) (Ref: Mail Thread
  42. log_format proxy '[$time_local] $upstream_cache_status $upstream_status $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] [Sent-to $server] "$http_user_agent" "$http_referer"';
  43. log_format standard '[$time_local] $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] "$http_user_agent" "$http_referer"';
  44. access_log /data/logs/default.log proxy;
  45. # Dynamically generated resolvers file
  46. include /etc/nginx/conf.d/include/resolvers.conf;
  47. # Default upstream scheme
  48. map $host $forward_scheme {
  49. default http;
  50. }
  51. # Real IP Determination
  52. # Docker subnet:
  53. set_real_ip_from 172.0.0.0/8;
  54. # NPM generated CDN ip ranges:
  55. include conf.d/include/ip_ranges.conf;
  56. # always put the following 2 lines after ip subnets:
  57. real_ip_header X-Forwarded-For;
  58. real_ip_recursive on;
  59. # Files generated by NPM
  60. include /etc/nginx/conf.d/*.conf;
  61. include /data/nginx/default_host/*.conf;
  62. include /data/nginx/proxy_host/*.conf;
  63. include /data/nginx/redirection_host/*.conf;
  64. include /data/nginx/dead_host/*.conf;
  65. include /data/nginx/temp/*.conf;
  66. # config git.thinxz.cn:80 | host -> 172.17.0.1
  67. server {
  68. listen 443;
  69. server_name git.thinxz.cn;
  70. ssl on;
  71. #ssl_certificate cert/2193315__sinoxx.com.pem;
  72. #ssl_certificate_key cert/2193315__sinoxx.com.key;
  73. ssl_session_timeout 5m;
  74. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  75. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  76. ssl_prefer_server_ciphers on;
  77. location / {
  78. proxy_http_version 1.1;
  79. proxy_set_header Upgrade $http_upgrade;
  80. proxy_set_header Connection upgrade;
  81. proxy_pass http://172.17.0.1:50001/;
  82. }
  83. }
  84. server {
  85. listen 80;
  86. server_name git.thinxz.cn;
  87. location / {
  88. proxy_pass http://127.0.0.1:50002/;
  89. proxy_set_header REMOTE-HOST $remote_addr;
  90. proxy_set_header Host $host;
  91. proxy_set_header X-Real-IP $remote_addr;
  92. }
  93. }
  94. }
  95. stream {
  96. # 添加socket转发的代理
  97. upstream socket_proxy {
  98. hash $remote_addr consistent;
  99. # 转发的目的地址和端口
  100. server 192.168.1.100:9000 weight=5 max_fails=3 fail_timeout=30s;
  101. }
  102. # 提供转发的服务,即访问localhost:9001, 会跳转至代理socket_proxy指定的转发地址
  103. server {
  104. listen 443;
  105. proxy_connect_timeout 1s;
  106. proxy_timeout 3s;
  107. proxy_pass socket_proxy;
  108. }
  109. }
  110. stream {
  111. # Files generated by NPM
  112. include /data/nginx/stream/*.conf;
  113. }
  114. //
  115. upstream git_pool{
  116. server 127.0.0.1:8081;
  117. }
  118. server {
  119. listen 80; #拦截端口
  120. server_name git.thinxz.cm; #域名配置
  121. access_log logs/git.log;
  122. error_log logs/git.error;
  123. # 将所有请求转发给git_pool池的应用处理
  124. location / {
  125. proxy_set_header Host $host;
  126. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  127. proxy_pass http://git_pool; #如果是ssl更改成https
  128. }
  129. }
  130. upstream git_pool{
  131. server 127.0.0.1:8081;
  132. }
  133. server {
  134. listen 80; #拦截端口
  135. server_name git.thinxz.cm; #域名配置
  136. access_log logs/git.log;
  137. error_log logs/git.error;
  138. # 将所有请求转发给git_pool池的应用处理
  139. location / {
  140. proxy_set_header Host $host;
  141. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  142. proxy_pass http://git_pool; #如果是ssl更改成https
  143. }
  144. }

nginx.conf

  1. [nginx.conf]
  2. user thinxzNginx thinxz123456;
  3. worker_processes 2; #设置值和CPU核心数一致
  4. error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
  5. pid /usr/local/webserver/nginx/nginx.pid;
  6. #Specifies the value for maximum file descriptors that can be opened by this process.
  7. worker_rlimit_nofile 65535;
  8. events
  9. {
  10. use epoll;
  11. worker_connections 65535;
  12. }
  13. http
  14. {
  15. include mime.types;
  16. default_type application/octet-stream;
  17. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  18. '$status $body_bytes_sent "$http_referer" '
  19. '"$http_user_agent" $http_x_forwarded_for';
  20. #charset gb2312;
  21. server_names_hash_bucket_size 128;
  22. client_header_buffer_size 32k;
  23. large_client_header_buffers 4 32k;
  24. client_max_body_size 8m;
  25. sendfile on;
  26. tcp_nopush on;
  27. keepalive_timeout 60;
  28. tcp_nodelay on;
  29. fastcgi_connect_timeout 300;
  30. fastcgi_send_timeout 300;
  31. fastcgi_read_timeout 300;
  32. fastcgi_buffer_size 64k;
  33. fastcgi_buffers 4 64k;
  34. fastcgi_busy_buffers_size 128k;
  35. fastcgi_temp_file_write_size 128k;
  36. gzip on;
  37. gzip_min_length 1k;
  38. gzip_buffers 4 16k;
  39. gzip_http_version 1.0;
  40. gzip_comp_level 2;
  41. gzip_types text/plain application/x-javascript text/css application/xml;
  42. gzip_vary on;
  43. #limit_zone crawler $binary_remote_addr 10m;
  44. #下面是server虚拟主机的配置
  45. server
  46. {
  47. listen 80;#监听端口
  48. server_name localhost;#域名
  49. index index.html index.htm index.php;
  50. root /usr/local/webserver/nginx/html;#站点目录
  51. location ~ .*\.(php|php5)?$
  52. {
  53. #fastcgi_pass unix:/tmp/php-cgi.sock;
  54. fastcgi_pass 127.0.0.1:9000;
  55. fastcgi_index index.php;
  56. include fastcgi.conf;
  57. }
  58. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
  59. {
  60. expires 30d;
  61. # access_log off;
  62. }
  63. location ~ .*\.(js|css)?$
  64. {
  65. expires 15d;
  66. # access_log off;
  67. }
  68. access_log off;
  69. }
  70. }