以RedHat7(64bit)平台为例

如果RedHat源没法用,可以使用EPEL源

  1. # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  2. # yum makecache
  3. # yum install gcc --enablerepo=epel (指定使用epel源)

IP信息清单:

Nginx_Master: 192.168.136.201   提供负载均衡
Nginx_BackUp: 192.168.136.202   负载均衡备机

Nginx_VIP: 192.168.136.200 网站的 VIP 地址(虚拟 IP)

1.安装Keepalived(Nginx主从双机热备)

  1. 安装依赖库
    # yum install -y wget gcc openssl-devel popt-devel
  2. 下载解压Keepalived
    # cd /usr/local/src
    # wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
    # tar -zxvf keepalived-1.2.19.tar.gz && cd keepalived-1.2.19
  3. 编译安装Keepalived
    # ./configure --sysconf=/etc
    # make && make install
    # ln -s /usr/local/sbin/keepalived /usr/sbin/keepalived
  4. 修改配置文件
    # vi /etc/keepalived/keepalived.conf
    主Nginx server上的keepalived.conf文件
    1. ! Configuration File for keepalived
    2.  
    3. global_defs {
    4. notification_email {
    5. admin@example.com
    6. }
    7. notification_email_from admin@example.com
    8. smtp_server 127.0.0.1
    9. smtp_connect_timeout 30
    10. router_id LVS_DEVEL
    11. }
    12.  
    13. vrrp_script check_run {
    14. script "/usr/local/bin/check_nginx.sh"
    15. interval 2
      weight 2
    16. }
    17.  
    18. vrrp_instance VI_1 {
    19. state MASTER
    20. interface eno16777728
    21. virtual_router_id 51
    22. priority 100
    23. advert_int 1
    24. authentication {
    25. auth_type PASS
    26. auth_pass 1111
    27. }
    28. track_script {
    29. check_run
    30. }
    31. virtual_ipaddress {
    32. 192.168.136.200
    33. }
    34. }

    备Nginx server上的keepalived.conf文件

    1. ! Configuration File for keepalived
    2.  
    3. global_defs {
    4. notification_email {
    5. admin@example.com
    6. }
    7. notification_email_from admin@example.com
    8. smtp_server 127.0.0.1
    9. smtp_connect_timeout 30
    10. router_id LVS_DEVEL
    11. }
    12.  
    13. vrrp_script check_run {
    14. script "/usr/local/bin/check_nginx.sh"
    15. interval 5
    16. }
    17.  
    18. vrrp_instance VI_1 {
    19. state BACKUP
    20. interface eno16777728
    21. virtual_router_id 51
    22. priority 99
    23. advert_int 1
    24. authentication {
    25. auth_type PASS
    26. auth_pass 1111
    27. }
    28. track_script {
    29. check_run
    30. }
    31. virtual_ipaddress {
    32. 192.168.136.200
    33. }
    34. }

    # vi /usr/local/bin/check_nginx.sh
    # chmod +x /usr/local/bin/check_nginx.sh

    1. #!/bin/bash
    2.  
    3. if [ "$(ps -ef | grep "nginx: master process"| grep -v grep)" == "" ]
    4. then
    5. service nginx start
    6. sleep 5
    7. if [ "$(ps -ef | grep "nginx: master process"| grep -v grep)" == "" ]
    8. then
    9. service keepalived stop
    10. fi
    11. fi
  5. 设置Keepalived服务开机自启动并启动服务
    # chkconfig keepalived on
    # service keepalived start

2.安装Nginx代理服务器安步骤

  1. 安装jemalloc(更好的内存管理)

    1. # yum -y install bzip2
      # cd /usr/local/src
    2. # wget http://www.canonware.com/download/jemalloc/jemalloc-4.0.4.tar.bz2
    3. # tar -jxvf jemalloc-4.0..tar.bz2 && cd jemalloc-4.0.
    4. # ./configure
    5. # make && make install
    6. # echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
    7. # ldconfig
  2. lua-nginx-module模块(Nginx支持lua语法的模块)
    lua-nginx-module来自大牛agentzh的开源项目,在Nginx中嵌入Lua语言,使之可以支持强大Lua语法
    1. . 下载LuaJIT2.0并安装
    2. # cd /usr/local/src
    3. # wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
    4. # tar -zxvf LuaJIT-2.0..tar.gz && cd LuaJIT-2.0.
    5. # make && make install
      # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
    1. 2. 导入环境变量
    2. # export LUAJIT_LIB=/usr/local/lib
    3. # export LUAJIT_INC=/usr/local/include/luajit-2.0
    1. 3. 下载并解压ngx_devel_kitlua-nginx-module
      # cd /usr/local/src
      # curl -L https://codeload.github.com/simpl/ngx_devel_kit/tar.gz/v0.2.19 -o ngx_devel_kit-0.2.19.tar.gz
      # tar -zxvf ngx_devel_kit-0.2..tar.gz
    2. # curl -L https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.9.20rc2 -o lua-nginx-module-0.9.20rc2.tar.gz
      # tar -zxvf lua-nginx-module-0.9.20rc2.tar.gz
  3. ngx_cache_purge模块(Nginx清除缓存的模块)
    1. # cd /usr/local/src
    2. # wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
    3. # tar -zxvf ngx_cache_purge-2.3.tar.gz
  4. 安装Nginx
    1. # yum -y install pcre-devel openssl-devel zlib-devel
      # wget http://nginx.org/download/nginx-1.9.9.tar.gz
    2. # tar -zxvf nginx-1.9..tar.gz && cd nginx-1.9.
    3. # ./configure \
    4. --sbin-path=/usr/local/nginx/nginx \--pid-path=/var/run/nginx.pid \
    5. --user=nginx \
    6. --group=nginx \
      --http-client-body-temp-path=/usr/local/nginx/cache/client_body_temp \
      --http-proxy-temp-path=/usr/local/nginx/cache/proxy_temp \
      --http-fastcgi-temp-path=/usr/local/nginx/cache/fastcgi_temp \
      --http-uwsgi-temp-path=/usr/local/nginx/cache/uwsgi_temp \
      --http-scgi-temp-path=/usr/local/nginx/cache/scgi_temp \
    7. --with-http_ssl_module \
    8. --with-http_stub_status_module \
    9. --with-threads \
    10. --with-stream \
    11. --with-stream_ssl_module \
    12. --with-ipv6 \
    13. --with-http_v2_module \
    14. --add-module=../ngx_cache_purge-2.3 \
    15. --add-module=../lua-nginx-module-0.9.20rc2 \
    16. --add-module=../ngx_devel_kit-0.2. \
    17. --with-ld-opt='-ljemalloc' \
    18. --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
      # make -j2 && make install
      # mkdir /usr/local/nginx/cache
      # ln -s /usr/local/nginx/nginx /usr/sbin/nginx (创建nginx可执行程序软链接)

    使用以下命令确认Nginx的SNI支持是否开启了:
    #nginx -V

  5. 创建Nginx启动脚本
    # vi /etc/init.d/nginx
    1. #!/bin/sh
    2. #
    3. # nginx - this script starts and stops the nginx daemon
    4. #
    5. # chkconfig: -
    6. # description: NGINX is an HTTP(S) server, HTTP(S) reverse \
    7. # proxy and IMAP/POP3 proxy server
    8. # processname: nginx
    9. # config: /etc/nginx/nginx.conf
    10. # config: /etc/sysconfig/nginx
    11. # pidfile: /var/run/nginx.pid
    12.  
    13. # Source function library.
    14. . /etc/rc.d/init.d/functions
    15.  
    16. # Source networking configuration.
    17. . /etc/sysconfig/network
    18.  
    19. # Check that networking is up.
    20. [ "$NETWORKING" = "no" ] && exit
    21.  
    22. nginx="/usr/local/nginx/nginx"
    23. prog=$(basename $nginx)
    24.  
    25. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    26.  
    27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    28.  
    29. lockfile=/var/lock/subsys/nginx
    30.  
    31. make_dirs() {
    32. # make required directories
    33. user=`$nginx -V >& | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
    34. if [ -z "`grep $user /etc/passwd`" ]; then
    35. useradd -r -M -s /sbin/nologin $user
    36. fi
    37. options=`$nginx -V >& | grep 'configure arguments:'`
    38. for opt in $options; do
    39. if [ `echo $opt | grep '.*-temp-path'` ]; then
    40. value=`echo $opt | cut -d "=" -f `
    41. if [ ! -d "$value" ]; then
    42. # echo "creating" $value
    43. mkdir -p $value && chown -R $user $value
    44. fi
    45. fi
    46. done
    47. }
    48.  
    49. start() {
    50. [ -x $nginx ] || exit
    51. [ -f $NGINX_CONF_FILE ] || exit
    52. make_dirs
    53. echo -n $"Starting $prog: "
    54. daemon $nginx -c $NGINX_CONF_FILE
    55. retval=$?
    56. echo
    57. [ $retval -eq ] && touch $lockfile
    58. return $retval
    59. }
    60.  
    61. stop() {
    62. echo -n $"Stopping $prog: "
    63. killproc $prog -QUIT
    64. retval=$?
    65. echo
    66. [ $retval -eq ] && rm -f $lockfile
    67. return $retval
    68. }
    69.  
    70. restart() {
    71. configtest || return $?
    72. stop
    73. sleep
    74. start
    75. }
    76.  
    77. reload() {
    78. configtest || return $?
    79. echo -n $"Reloading $prog: "
    80. killproc $nginx -HUP
    81. RETVAL=$?
    82. echo
    83. }
    84.  
    85. force_reload() {
    86. restart
    87. }
    88.  
    89. configtest() {
    90. $nginx -t -c $NGINX_CONF_FILE
    91. }
    92.  
    93. rh_status() {
    94. status $prog
    95. }
    96.  
    97. rh_status_q() {
    98. rh_status >/dev/null >&
    99. }
    100.  
    101. case "$1" in
    102. start)
    103. rh_status_q && exit
    104. $
    105. ;;
    106. stop)
    107. rh_status_q || exit
    108. $
    109. ;;
    110. restart|configtest)
    111. $
    112. ;;
    113. reload)
    114. rh_status_q || exit
    115. $
    116. ;;
    117. force-reload)
    118. force_reload
    119. ;;
    120. status)
    121. rh_status
    122. ;;
    123. condrestart|try-restart)
    124. rh_status_q || exit
    125. ;;
    126. *)
    127. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    128. exit
    129. esac
  6. 设置Nginx服务开机自启动并启动服务
    1. # chmod +x /etc/init.d/nginx
    2. # chkconfig nginx on
      # service nginx start
  7. 开通http,https防火墙端口
    1. # firewall-cmd --permanent --add-service={http,https}
    2. # firewall-cmd --reload
  8. 在浏览器中测试Nginx

2.生成SSL证书步骤

  1. 创建证书存放目录并切换到该目录
    # mkdir -p /usr/local/nginx/conf/ssl && cd /usr/local/nginx/conf/ssl

使用openssl生成服务器证书

假设我们有两个站点linux.example.com,windows.example.com
     Domain                          UpStream                                         Servers                                                        System
--------------------------     ----------------------------     ----------------------------------------------------------------        -------------------
linux.example.com           linux.example.com            192.168.136.101,192.168.136.102,192.168.136.103               Linux
windows.example.com      windows.example.com       192.168.136.104,192.168.136.105                                       Windows

以linux.example.com为例,生成服务器证书

  1. 生成服务器端的私钥(key文件)
    # openssl genrsa -des3 -out linux.example.com.key 1024

    1. Generating RSA private key, bit long modulus
    2. ...........++++++
    3. .....................++++++
    4. e is (0x10001)
    5. Enter pass phrase for linux.example.com.key: <口令>
      Verifying - Enter pass phrase for linux.example.com.key: <确认口令>
  2. 创建证书签名请求Certificate Signing Request (CSR)
    # SUBJECT="/C=CN/ST=China/L=Shanghai/O=example.com/OU=example.com/CN=linux.example.com"
    # openssl req -new -subj $SUBJECT -key linux.example.com.key -out linux.example.com.csr
    1. Enter pass phrase for secure1.example.com.key: <确认口令>
  3. 清除重启Nginx服务时提示必须输入密钥
    # mv linux.example.com.key linux.example.com.origin.key
    # openssl rsa -in linux.example.com.origin.key -out linux.example.com.key
  4. 使用刚生成的私钥和CSR创建自签名的CA证书
    # openssl x509 -req -days 3650 -in linux.example.com.csr -signkey linux.example.com.key -out linux.example.com.crt
  5. 重复上面操作,生成windows.example.com证书

创建Nginx配置文件

  1. 创建upstream配置文件
    # mkdir /usr/local/nginx/conf/upstreams && cd /usr/local/nginx/conf/upstreams
    # vi linux.example.com.conf

    1. upstream linux.example.com {
    2. ip_hash;
    3. server 192.168.136.101:;
    4. server 192.168.136.102:;
    5. server 192.168.136.103:;
    6. }

    # vi windows.example.com.conf

    1. upstream windows.example.com {
    2. ip_hash;
    3. server 192.168.136.104:;
    4. server 192.168.136.105:;
    5. }
  2. 安装nginx_ensite工具
    # cd /usr/local/src
    # yum -y install git
    # git clone https://github.com/perusio/nginx_ensite.git && cd nginx_ensite
    # make install
    修改nginx_ensite脚本
    # vi /usr/local/bin/nginx_ensite
    1. #!/bin/bash
    2.  
    3. ### nginx_ensite --- Bash script to enable or disable a site in nginx.
    4.  
    5. ### Copyright (C) , António P. P. Almeida <appa@perusio.net>
    6.  
    7. ### Author: António P. P. Almeida <appa@perusio.net>
    8.  
    9. ### Permission is hereby granted, free of charge, to any person obtaining a
    10. ### copy of this software and associated documentation files (the "Software"),
    11. ### to deal in the Software without restriction, including without limitation
    12. ### the rights to use, copy, modify, merge, publish, distribute, sublicense,
    13. ### and/or sell copies of the Software, and to permit persons to whom the
    14. ### Software is furnished to do so, subject to the following conditions:
    15.  
    16. ### The above copyright notice and this permission notice shall be included in
    17. ### all copies or substantial portions of the Software.
    18.  
    19. ### Except as contained in this notice, the name(s) of the above copyright
    20. ### holders shall not be used in advertising or otherwise to promote the sale,
    21. ### use or other dealings in this Software without prior written authorization.
    22.  
    23. ### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24. ### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25. ### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    26. ### THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27. ### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    28. ### FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    29. ### DEALINGS IN THE SOFTWARE.
    30.  
    31. SCRIPTNAME=${##*/}
    32.  
    33. ## The nginx binary. Check if we're root or not. If we are get the
    34. ## path to nginx. If not hardcode the path.
    35. if [ $(id -u) -eq ]; then
    36. IS_ROOT=
    37. NGINX=$(command -v nginx) || exit
    38. else
    39. STATUS=
    40. NGINX=/usr/sbin/nginx
    41. fi
    42.  
    43. ## Default value for the configuration directory.
    44. NGINX_CONF_DIR=/usr/local/nginx/conffunction print_usage() {
    45. echo "$SCRIPTNAME [-c <nginx configuration base directory> default: /usr/local/nginx/conf] [ -s <startup program name> default: nginx] <site name>"
    46. }
    47.  
    48. ## Extract the startup program name from a given argument. If it's a
    49. ## path to nginx then add the '-s reload' to the name. Otherwise just
    50. ## return the given argument.
    51. ## $: the program name.
    52. ## Returns the proper startup program name,
    53. function get_startup_program_name() {
    54. local value="$1"
    55.  
    56. [[ $ =~ [[:alnum:]/-]*nginx$ ]] && value="$1 -s reload"
    57.  
    58. echo "$value"
    59. }
    60.  
    61. ## The default start up program is nginx.
    62. STARTUP_PROGRAM_NAME=$(get_startup_program_name nginx)
    63.  
    64. ## Create the relative path to the vhost file.
    65. ## $: configuration file name (usually the vhost)
    66. ## $: available sites directory name (usually sites-available)
    67. ## Returns the relative path from the sites-enabled directory.
    68. function make_relative_path() {
    69. printf '../%.0s%s/%s' $(eval echo {..$(expr length "${1//[^\/]/}")}) $ $
    70. }
    71.  
    72. ## Checking the type of action we will perform. Enabling or disabling.
    73. ACTION=$(echo $SCRIPTNAME | awk '$0 ~ /dissite/ {print "DISABLE"} $0 ~ /ensite/ {print "ENABLE"} $0 !~ /(dis|en)site/ {print "UNKNOWN"}')
    74.  
    75. if [ "$ACTION" == "UNKNOWN" ]; then
    76. echo "$SCRIPTNAME: Unknown action!" >&
    77. print_usage
    78. exit
    79. fi
    80.  
    81. ## Check the number of arguments.
    82. if [ $# -lt -o $# -gt ]; then
    83. print_usage >&
    84. exit
    85. fi
    86.  
    87. ## Parse the getops arguments.
    88. while getopts c:s: OPT; do
    89. case $OPT in
    90. c|+c)
    91. NGINX_CONF_DIR=$(realpath "$OPTARG")
    92. if [[ ! -d $NGINX_CONF_DIR ]]; then
    93. echo "$NGINX_CONF_DIR directory not found." >&
    94. exit
    95. fi
    96. ;;
    97. s|+s)
    98. STARTUP_PROGRAM_NAME=$(get_startup_program_name "$OPTARG")
    99. ;;
    100. *)
    101. print_usage >&
    102. exit
    103. ;;
    104. esac
    105. done
    106. shift $(( OPTIND - ))
    107. OPTIND=
    108.  
    109. ## The paths for both nginx configuration files and the sites
    110. ## configuration files and symbolic link destinations.
    111. AVAILABLE_SITES_PATH="$NGINX_CONF_DIR/sites-available"
    112. ENABLED_SITES_PATH="$NGINX_CONF_DIR/sites-enabled"
    113.  
    114. ## Check the number of arguments.
    115. if [ $# -ne ]; then
    116. print_usage >&
    117. exit
    118. else
    119. SITE_AVAILABLE=$(make_relative_path "$1" ${AVAILABLE_SITES_PATH##*/})
    120.  
    121. ## If enabling the 'default' site then make sure that it's the
    122. ## first to be loaded.
    123. if [ "$1" == "default" ]; then
    124. SITE_ENABLED="$ENABLED_SITES_PATH/default"
    125. else
    126. SITE_ENABLED="$ENABLED_SITES_PATH/$1"
    127. fi
    128. ## Check if the directory where we will place the symlink
    129. ## exists. If not create it.
    130. [ -d ${SITE_ENABLED%/*} ] || mkdir -p ${SITE_ENABLED%/*}
    131. fi
    132.  
    133. ## Check that the file corresponding to site exists if enabling or
    134. ## that the symbolic link exists if disabling. Perform the desired
    135. ## action if possible. If not signal an error and exit.
    136. case $ACTION in
    137. ENABLE)
    138. # Change to the directory where we will place the symlink so that we
    139. # see the relative path correctly.
    140. cd "${SITE_ENABLED%/*}";
    141. if [ -r $SITE_AVAILABLE ]; then
    142. ## Test for a well formed configuration only when we are
    143. ## root.
    144. if [ -n "$IS_ROOT" ]; then
    145. echo "Testing nginx configuration..."
    146. $NGINX -t && STATUS=0
    147. fi
    148. ## Check the config testing status and if the link exists already.
    149. if [ $STATUS ] && [ -h $SITE_ENABLED ]; then
    150. ## If already enabled say it and exit.
    151. echo "$1 is already enabled."
    152. exit 0
    153. else # Symlink if not yet enabled.
    154. ln -s $SITE_AVAILABLE $SITE_ENABLED
    155. fi
    156. if [ $STATUS ]; then
    157. echo -n "Site $1 has been enabled."
    158. printf '\nRun "%s" to apply the changes.\n' "$STARTUP_PROGRAM_NAME"
    159. exit 0
    160. else
    161. exit 5
    162. fi
    163. else
    164. echo "Site configuration file $1 not found." >&2
    165. exit 6
    166. fi
    167. ;;
    168. DISABLE)
    169. if [ "$1" = "default" ] ; then
    170. if [ -h "$ENABLED_SITES_PATH/default" ] ; then
    171. SITE_ENABLED="$ENABLED_SITES_PATH/default"
    172. fi
    173. fi
    174. if [ -h $SITE_ENABLED ]; then
    175. rm $SITE_ENABLED
    176. echo -n "Site $1 has been disabled."
    177. printf '\nRun "%s" to apply the changes.\n' "$STARTUP_PROGRAM_NAME"
    178. exit 0
    179. else
    180. echo "Site $1 doesn't exist." >&2
    181. exit 7
    182. fi
    183. ;;
    184. esac
  3. 创建sites-available目录并进入
    # mkdir /usr/local/nginx/conf/sites-available && cd /usr/local/nginx/conf/sites-available
  4. 创建站点配置文件
    # vi no-default
    1. # Drop requests for unknown hosts
    2. #
    3. # If no default server is defined, nginx will use the first found server.
    4. # To prevent host header attacks, or other potential problems when an unknown
    5. # servername is used in a request, it's recommended to drop the request
    6. # returning "no response".
    7.  
    8. server {
    9. listen default_server;
    10. return ;
    11. }

    # vi linux.example.com

    1. server {
    2. listen [::]:;
    3. listen ;
    4. server_name linux.example.com;
    5.  
    6. return https://$host$request_uri;
    7. }
    8.  
    9. server {
    10. listen [::]: ssl http2;
    11. listen ssl http2;
    12. server_name linux.example.com;
    13.  
    14. access_log logs/linux.example.com.access.log main;
    15. error_log logs/linux.example.com.error.log error;
    16.  
    17. location / {
    18. proxy_pass http://linux.example.com;
    19. }
    20.  
    21. include ssl.conf;
    22.  
    23. ssl_certificate ssl/linux.example.com.crt;
    24. ssl_certificate_key ssl/linux.example.com.key;
    25. }

    # vi windows.example.com

    1. server {
    2. listen [::]:;
    3. listen ;
    4. server_name windows.example.com;
    5.  
    6. return https://$host$request_uri;
    7. }
    8.  
    9. server {
    10. listen [::]: ssl http2;
    11. listen ssl http2;
    12. server_name windows.example.com;
    13.  
    14. access_log logs/windows.example.com.access.log main;
    15. error_log logs/windows.example.com.error.log error;
    16.  
    17. location / {
    18. proxy_pass http://windows.example.com;
    19. }
    20.  
    21. include ssl.conf;
    22.  
    23. ssl_certificate ssl/windows.example.com.crt;
    24. ssl_certificate_key ssl/windows.example.com.key;
    25. }
  5. 启用站点和禁用站点的方法
    # nginx_ensite linux.example.com   (启用站点)
    # nginx_dissite linux.example.com    (禁用站点)
  6. 创建zone.conf配置文件
    # vi /usr/local/nginx/conf/zone.conf
    1. #1mb zone holds approx 16k sessions
    2. #Connections per IP
    3. limit_conn_zone $binary_remote_addr zone=conPerIp:5m;
    4.  
    5. # Fastcgi cache zones below
    6. # At some point you'd probably want to change these paths to their own
    7. # directory, for example to /var/cache/nginx/
      fastcgi_cache_path /usr/local/nginx/cache/fastcgi_cache levels=: keys_zone=fastcgi_cache:16m max_size=256m inactive=1d;
    8.  
    9. limit_req_zone $binary_remote_addr zone=reqPerSec1:1m rate=1r/s;
    10. limit_req_zone $binary_remote_addr zone=reqPerSec10:1m rate=10r/s;
    11. limit_req_zone $binary_remote_addr zone=reqPerSec20:1m rate=20r/s;
  7. 创建proxy.conf配置文件
    # vi /usr/local/nginx/conf/proxy.conf
    1. proxy_redirect off;
    2. proxy_set_header Host $host;
    3. proxy_set_header X-Real-IP $remote_addr;
    4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    5.  
    6. proxy_connect_timeout 30;
    7. proxy_send_timeout 30;
    8. proxy_read_timeout 60;
    9. proxy_buffer_size 256k;
    10. proxy_buffers 4 256k;
    11. proxy_busy_buffers_size 256k;
    12. proxy_temp_file_write_size 256k;
    13. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    14. proxy_max_temp_file_size 128m;
  8. 创建ssl.conf配置文件
    # vi /usr/local/nginx/conf/ssl.conf
    1. add_header Strict-Transport-Security 'max-age=604800';
    2.  
    3. ssl_session_cache shared:SSL:10m;
    4. ssl_session_timeout 10m;
    5. ssl_prefer_server_ciphers on;
    6. ssl_protocols TLSv1 TLSv1. TLSv1.;
    7.  
    8. # Maximum secure cipher list from https://cipherli.st/. Not support some clients: IF6/XP, IE8/XP, Java 6u45, Java 7u25, OpenSSL 0.9.8y
    9. ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    10.  
    11. # Less secure cipher list from https://cipherli.st/. Not support some clients: IF6/XP, Java 6u45
    12. #ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES128-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA128:DHE-RSA-AES128-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA128:ECDHE-RSA-AES128-SHA384:ECDHE-RSA-AES128-SHA128:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA384:AES128-GCM-SHA128:AES128-SHA128:AES128-SHA128:AES128-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
  9. 创建gzip.conf配置文件
    # vi /usr/local/nginx/conf/gzip.conf
    1. gzip on;
    2. gzip_http_version 1.0;
    3. gzip_min_length ;
    4. gzip_buffers 8k;
    5. gzip_proxied expired no-cache no-store private auth;
    6. gzip_disable "msie6";
    7. gzip_vary on;
    8. gzip_comp_level ;
    9. gzip_types
    10. # text/html is always compressed by HttpGzipModule
    11. text/css
    12. text/javascript
    13. text/xml
    14. text/plain
    15. text/x-component
    16. application/javascript
    17. application/x-javascript
    18. application/json
    19. application/xml
    20. application/rss+xml
    21. application/atom+xml
    22. font/truetype
    23. font/opentype
    24. application/vnd.ms-fontobject
    25. image/svg+xml;
  10. 修改nginx.conf配置文件
    # vi /usr/local/nginx/conf/nginx.conf
    1. user nginx;
    2. worker_processes auto;
    3.  
    4. worker_rlimit_nofile ;
    5.  
    6. events {
    7. worker_connections ;
    8. }
    9.  
    10. error_log logs/error.log warn;
    11.  
    12. http {
    13. include mime.types;
    14. default_type text/html;
    15. server_tokens off;
    16. msie_padding off;
    17. max_ranges ;
    18. charset utf-;
    19. reset_timedout_connection on;
    20. keepalive_disable none;
    21.  
    22. sendfile on;
    23. tcp_nopush on;
    24. tcp_nodelay off;
    25. keepalive_requests ;
    26.  
    27. log_format main '$remote_addr $scheme://$host $remote_user [$time_local] "$request" '
    28. '$status $body_bytes_sent "$http_referer" '
    29. '"$http_user_agent" $request_time $upstream_addr $upstream_cache_status';
    30. log_subrequest on;
    31.  
    32. variables_hash_max_size ;
    33. map_hash_max_size ;
    34. server_names_hash_max_size ;
    35. types_hash_max_size ;
    36.  
    37. open_file_cache max=;
    38. open_file_cache_errors on;
    39.  
    40. keepalive_timeout ;
    41. client_header_timeout ;
    42. client_body_timeout ;
    43. send_timeout ;
    44.  
    45. fastcgi_connect_timeout ;
    46. fastcgi_send_timeout ;
    47.  
    48. include proxy.conf;
      include zone.conf;
    49. include upstreams/*.conf;
    50. include sites-enabled/*;
    51. }

    生成证书的脚本: 

    1. #!/bin/sh
    2.  
    3. # create self-signed server certificate:
    4.  
    5. read -p "Enter your domain [www.example.com]: " DOMAIN
    6.  
    7. echo "Create server key..."
    8.  
    9. openssl genrsa -des3 -out $DOMAIN.key
    10.  
    11. echo "Create server certificate signing request..."
    12.  
    13. SUBJECT="/C=CN/ST=China/L=Shanghai/O=example.com/OU=example.com/CN=$DOMAIN"
    14.  
    15. openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr
    16.  
    17. echo "Remove password..."
    18.  
    19. mv $DOMAIN.key $DOMAIN.origin.key
    20. openssl rsa -in $DOMAIN.origin.key -out $DOMAIN.key
    21.  
    22. echo "Sign SSL certificate..."
    23.  
    24. openssl x509 -req -days -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
    25.  
    26. echo "TODO:"
    27. echo "Copy $DOMAIN.crt to /usr/local/nginx/conf/ssl/$DOMAIN.crt"
    28. echo "Copy $DOMAIN.key to /usr/local/nginx/conf/ssl/$DOMAIN.key"
      echo "Add configuration in nginx:"
    29. echo "server {"
    30. echo " ..."
    31. echo " listen 443 ssl;"
    32. echo " ssl_certificate /usr/local/nginx/conf/ssl/$DOMAIN.crt;"
      echo " ssl_certificate_key /usr/local/nginx/conf/ssl/$DOMAIN.key;"
      echo "}"

RedHat7配置Nginx实现多域名虚拟主机的SSL/TLS认证(实现单IP以不同证书服务于不同域名)的更多相关文章

  1. HTTPS-SSL/TSL与SNI的关系以及同IP多域名虚拟主机的SSL/TSL认证

    早期的SSLv2根据经典的公钥基础设施PKI(Public Key Infrastructure)设计,它默认认为:一台服务器(或者说一个IP)只会提供一个服务,所以在SSL握手时,服务器端可以确信客 ...

  2. Apache服务器在80端口配置多域名虚拟主机的方法

    我们在配置一台服务器的时候,如果只运行一个站点,往往过于浪费资源.Nginx和Apache都可以通过配置虚拟主机实现多站点.配置虚拟主机的方式主要有两种,一种是多个不同端口对应的多个虚拟主机站点,一种 ...

  3. 高级运维(二):搭建Nginx服务器、用户认证、基于域名的虚拟主机、SSL虚拟主机、Nginx反向代理

    一.搭建Nginx服务器 目标: 在IP地址为192.168.4.5的主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能: 1> SSL加密功能 2> 设置Ng ...

  4. lvs,nginx反向代理,虚拟主机

    LVS NAT 拓扑 client | | LVS | | ------------------- | | | RS1 RS2 RS3 地址规划如下 机器名称 ip配置 ip配置 备注信息 LVS 1 ...

  5. nginx的应用【虚拟主机】

    Nginx主要应用: 静态web服务器 负载均衡 静态代理虚拟主机 虚拟主机 :虚拟主机,就是把一台物理服务器划分成多个“虚拟”的服务器,这样我们的一台物理服务器就可以当做多个服务器来使用,从而可以配 ...

  6. nginx之全局设置,location,虚拟主机,日志管理

    nginx之全局设置,location,虚拟主机,日志管理 worker_processes 1;//子进程,cpu数*核数 ****************全局设置************** ** ...

  7. Apache+php+mysql的安装与配置 - 之三(Apache的虚拟主机配置)

    Apache+php+mysql的安装与配置 - 之三(Apache的虚拟主机配置) Apache核心(Core)配置 VirtualHost 语法 <VirtualHost addr[:por ...

  8. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  9. 简单配置Nginx 指向本地端口,并开启SSL

    简单配置Nginx 指向本地端口,并开启SSL,如果要开启SSL,必须使用域名去申请SSL key,一般是两个文件,一般是要收费的. # 在/etc/nginx/nginx.conf 的文件中有下面一 ...

随机推荐

  1. Java RMI简单例子HelloWorld

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

  2. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...

  3. [wikioi]数的划分

    http://wikioi.com/problem/1039/ 划分型DP.最终的思路是,F[i][j]表示i分成j份,如果分出来的有1,那么去掉1,就是F[i-1][j-1]:如果没有1,那就都减1 ...

  4. ANDROID_MARS学习笔记_S03_006_geocoding、HttpClient

    一.简介 二.代码1.xml(1)AndroidManifest.xml <uses-permission android:name="android.permission.ACCES ...

  5. Android开源项目发现---ListView篇(持续更新)

    资料转载地址:https://github.com/Trinea/android-open-project 1. android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下 ...

  6. redhat 6.5 使用其它Linux镜像源的yum源

    最近在虚拟机里装了rhel_6.5_x86_64,发现竟然不自带g++,没办法只好 “yum install gcc-c++”,无奈失败,原因是redhat的yum是收费的... 于是打算怒装其它免费 ...

  7. 在Azure中使用Load Runner测试TCP最大并发连接数

    对于Azure中的每一台虚机,它所能支持的TCP最大并发连接数是50万(参考微软官网: http://azure.microsoft.com/en-us/documentation/articles/ ...

  8. spm_预处理实验记录

    参考:<SPM8 MANNUAL> Chapter 28 Auditory fMRI data

  9. HDU-1518 Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  10. HDU-1233 还是畅通工程 (prim 算法求最小生成树)

    prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...