1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| 一、安装Nginx useradd www -M -s /sbin/nologin 1、安装依赖: yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libiconv libmcrypt mhash mcrypt libmcrypt-devel pcre-devel openssl-devel freetype-devel libcurl-devel 2、下载nginx: wget http://nginx.org/download/nginx-1.12.1.tar.gz 3、编译安装: ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-stream 添加第三方模块 --add-module=PATH
make make install clean
Nginx配置rsyslog
vim /usr/local/nginx/conf/nginx.conf 可以配置在http或者server段中,凡是可以配置access_log和error_log的位置都可以配置 # syslog表示使用syslog协议 # server=10.26.2.65 指明远程服务器地址,也可以指定本地 # facility=local7 指明设备管道使用local7 # tag=nginx 标签表示在日志文件中显示时候的标题 # severity=info 表示日志级别 access_log syslog:server=127.0.0.1,facility=local7,tag=nginx,severity=info;
vim /etc/rsyslog.conf 默认有下面的设置,会读取 /etc/rsyslog.d/*.conf 目录下的配置文件 $IncludeConfig /etc/rsyslog.d/*.conf 为nginx创建一个独立的配置文件 vim /etc/rsyslog.d/nginx.conf $ModLoad imudp $UDPServerRun 514 local7.* /var/log/nginx/access.log &~ #如果不加下面的的&~则除了在/var/log/nginx/access.log中写入日志外,也会写入message文件 配置rsyslog的主配置文件,开启远程日志 # vim /etc/sysconfig/rsyslog SYSLOGD_OPTIONS=”-c 2 -r -m 0″ #-c 2 使用兼容模式,默认是 -c 5 #-r 开启远程日志 #-m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
配置完成后重启nginx和rsyslog即可
系统优化: user www; worker_processes 8; worker_rlimit_nofile 1048576; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 102400; multi_accept on; use epoll; }
http { server_tokens off; include mime.types; default_type application/octet-stream; client_header_buffer_size 32k; keepalive_timeout 300s 300s; keepalive_requests 10000;
# log_format main '$remote_addr - $remote_user [$time_local] $http_host "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"' # '$upstream_addr $upstream_response_time $request_time ';
# log_format json '{"@timestamp":"$time_iso8601",' # '"@version":"1",' # '"host":"$server_addr",' # '"client":"$remote_addr",' # '"size":"$body_bytes_sent",' # '"responsetime":"$request_time",' # '"domain":"$http_host",' # '"request":"$request",' # '"status":"$status",' # '"upstream_addr":"$upstream_addr",' # '"upstream_response_time":"$upstream_response_time",' # '"request_time":"$request_time"' # '}'; log_format main '$time_local $remote_addr $status $server_addr $http_host "$request" $body_bytes_sent $http_referer $http_user_agent $upstream_addr $request'; log_format main '$time_local $server_addr $status $http_host $request_uri $args $http_referer $http_user_agent $remote_addr $body_bytes_sent $request_method $upstream_addr $server_port';
# access_log /var/log/nginx/access.log main; access_log syslog:server=127.0.0.1,facility=local1,tag=nginx,severity=info main;
sendfile on; tcp_nopush on; tcp_nodelay on;
#reset_timeout_connection on; #limit_conn_zone $binary_remote_addr zone=addr:5m; #limit_conn addr 100; gzip on; gzip_disable "msie6"; # gzip_static on; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 4; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_ignore_client_abort on;
server { listen 80; server_name localhost;
location /nginx_status { stub_status on; access_log off; }
location / { root html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
} include /usr/local/nginx/conf/conf.d/*.conf; }
net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 600 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 20 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 873200 net.core.wmem_max = 873200 net.ipv4.tcp_rmem = 32768 436600 873200 net.ipv4.tcp_wmem = 8192 436600 873200 net.core.netdev_max_backlog = 32768 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_retries2 = 5 net.ipv4.tcp_mem = 41943040 73400320 94371840 net.ipv4.tcp_max_orphans = 3276800 fs.file-max = 1300000 net.ipv4.ip_nonlocal_bind=1
二、安装confd 直接拷贝confd到/usr/local/bin/ 修改权限 chmod 755 /usr/local/bin/confd mkdir -p /etc/confd/{conf.d,templates} confd -version vim /etc/confd/templates/account.tmpl 1) 80 443共存 {{range $dir := lsdir "/web/account.italent.cn"}} upstream {{base $dir}} { {{$custdir := printf "/web/account.italent.cn/%s/*" $dir}}{{range gets $custdir}} server {{$data := json .Value}}{{$data.IP}}:80 {{$data.WEIGHT}}; {{end}} }
server { listen 80; listen 443 ssl; server_name {{base $dir}}; ssl_certificate /usr/local/nginx/certs/{{base $dir}}.crt; ssl_certificate_key /usr/local/nginx/certs/{{base $dir}}.key; access_log syslog:server=127.0.0.1,facility=local1,tag=nginx,severity=info; access_log /var/log/nginx/{{base $dir}}.access.log; error_log /var/log/nginx/{{base $dir}}.log; location / { proxy_pass http://{{base $dir}}; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } {{end}} 2) 80 强制跳转443 {{range $dir := lsdir "/web/account.italent.cn"}} upstream {{base $dir}} { {{$custdir := printf "/web/account.italent.cn/%s/*" $dir}}{{range gets $custdir}} server {{$data := json .Value}}{{$data.IP}}:80 {{$data.WEIGHT}}; {{end}} }
server { listen 80; server_name {{base $dir}}; return 301 https://$server_name$request_uri; access_log syslog:server=127.0.0.1,facility=local1,tag=nginx,severity=info; access_log /var/log/nginx/{{base $dir}}.access.log; error_log /var/log/nginx/{{base $dir}}.log; }
server { listen 443 ssl; server_name {{base $dir}}; ssl_certificate /usr/local/nginx/certs/{{base $dir}}.crt; ssl_certificate_key /usr/local/nginx/certs/{{base $dir}}.key; access_log syslog:server=127.0.0.1,facility=local1,tag=nginx,severity=info; access_log /var/log/nginx/{{base $dir}}.access.log; error_log /var/log/nginx/{{base $dir}}.log; location / { proxy_pass http://{{base $dir}}; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } {{end}}
vim /etc/confd/conf.d/account.toml [template] keys = [ "/web/account.italent.cn", ] owner = "nginx" mode = "0644" src = "account.tmpl" dest = "/usr/local/nginx/conf/conf.d/account.conf"
三、证书拷贝 /usr/local/nginx/certs
|