赞
踩
yum update
yum -y install vim gcc gcc-c++ automake autoconf libtool make pcre-devel openssl openssl-devel zlib zlib-devel
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -xzvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
mkdir module
nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar
nginx-http-concat-1.2.2.tar
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-1.16.0/module/nginx-http-concat-1.2.2/ --add-module=/usr/local/nginx-1.16.0/module/nginx-sticky-module/ --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module
make && make install
worker_processes 2; events { worker_connections 10240; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; gzip_min_length 2k; gzip_buffers 4 16k; gzip_comp_level 3; gzip_types text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf font/ttf font/eot image/jpeg image/gif image/png; upstream xxx { #使用sticky,不设置expires则浏览器关闭时结束会话 #sticky domain=xxx.zypcy.cn path=/; server localhost:8080; keepalive 32; keepalive_requests 100; keepalive_timeout 40s; } server { listen 80; server_name xxx.zypcy.cn; location / { proxy_pass http://xxx; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #如果swagger没有转发端口,则可以用下面的配置 #proxy_set_header Forwarded host=$host:$server_post; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; client_max_body_size 10m; client_body_buffer_size 256k; #下面2个配置参数在转发时比较重要,如果是api服务请注释,如果是页面服务请打开注释 #proxy_buffering off; #proxy_redirect off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #配置concat合并小文件功能 #http://192.168.68.136:8080/js/??a.js,b.js,c.js (同一目录下资源) #http://192.168.68.136:8080/??kissy/seed-min.js,kg/global-util/index-min.js(不同目录下资源) server { listen 80; server_name res.zypcy.cn; #允许跨域访问 add_header 'Access-Control-Allow-Origin' '*'; location / { #开启referers防盗链,只能 *.zypcy.cn 能访问该location下的资源 valid_referers none blocked server_names *.zypcy.cn; if ($invalid_referer) { #盗链时返回403 return 403; } alias /home/lkh/; index index.html index.htm; #开启小文件合并功能 concat on; concat_max_files 30; concat_types concat_types: text/css text/javascript application/javascript application/x-javascript; } } }
vim /etc/sysctl.conf
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
配置生效 :sysctl -p
vi /etc/profile
加入:ulimit -SHn 1048576
source /etc/profile
1.创建nginx目录结构:mkdir -p /home/nginx/logs /home/nginx/conf 2.给nginx目录授权:chmod 777 /home/nginx -R 3.启动nginx:docker run -d --name nginx nginx:1.16.1 4.把nginx容器中配置文件复制到宿主机: docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf docker cp nginx:/etc/nginx/conf.d/default.conf /home/nginx/conf 5.停止nginx:docker stop nginx; docker rm nginx; 6.启动nginx:docker run -d -it --name nginx --restart=always -p 7080:80 \ -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /home/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -v /home/nginx/logs:/var/log/nginx \ nginx:1.16.1 7.修改 /home/nginx/conf/default.conf 中的配置 8.热更新配置:docker exec -i nginx(容器id) nginx -s reload
根据自己的需求修改 vi /home/nginx/conf/default.conf 配置中的内容
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #root /usr/share/nginx/html; #index index.html index.htm; #default_type text/html; #return 200 'hello zy!'; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; #如果swagger没有转发端口,则可以用下面的配置 #proxy_set_header Forwarded host=$host:$server_post; client_max_body_size 50m; client_body_buffer_size 512k; proxy_connect_timeout 1; proxy_send_timeout 60; proxy_read_timeout 120; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
还可以修改 vi /home/nginx/conf/nginx.conf 的配置
user nginx; worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 10240; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_min_length 2k; gzip_buffers 4 16k; gzip_comp_level 3; gzip_types text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf font/ttf font/eot image/jpeg image/gif image/png; include /etc/nginx/conf.d/*.conf; }
–restart=always 表示docker重启时,容器也重启
–network=host 表示使用宿主机网络
docker stop nginx;docker rm nginx;docker run -d -it --name nginx \
-p 7080:80 \
-v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf \
-v /root/nginx/logs:/var/log/nginx \
nginx:1.16.1
server { listen 80; server_name 10.10.10.146; location / { #default_type text/html; #return 200 'hello zy!'; proxy_pass http://xxx; #如果swagger没有转发端口,则需要$server_port写死 proxy_set_header Forwarded host=$host:$server_port; #proxy_set_header X-Forwarded-Host $host; #如果是docker容器,则需要$server_port写死 #proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。