首页 > 技术文章 > nginx配置

sxck 2020-03-19 16:28 原文

线上案例实战(58月嫂):

user nobody;
#user root;
worker_processes 2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 4024;
}


http {
include 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 logs/access.log main;

sendfile on; 开启高效文件传输
keepalive_timeout 100; 客户端连接保持超时时间

#gzip on;

server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}


}
server{
listen 80;
server_name 4000512727.com;
location / {
proxy_pass http://4000512727.com:9536;
}
}
server{
listen 80;
server_name 58yuesao.sxsihe.com;
location / {
proxy_pass http://58yuesao.sxsihe.com:9539;
}
}
server{
listen 80;
server_name yuesao.sxsihe.com;
location / {
proxy_pass http://yuesao.sxsihe.com:9540;
}

}
server{
listen 80;
server_name 58ys.sxsihe.com;
location / {
proxy_pass http://58ys.sxsihe.com:9541;
}

}
server{
listen 8000;
server_name yuesao.sxsihe.com;
location / {
root /home/xieyw;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}

}

server {
listen 443;
server_name 4000512727.com;

ssl on;
ssl_certificate /opt/58wyh/nginx/conf/server.pem;
ssl_certificate_key /opt/58wyh/nginx/conf/server.key;

ssl_session_timeout 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM;
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://4000512727.com:9536;
}
}

}


线上案例实战
user wyp;
worker_processes 4; //4线程
worker_cpu_affinity 0001 0010 0100 1000; //4核cpu一次对应第一,二,三,四线程。
worker_rlimit_nofile 131072; //更改进程的最大打开文件数限制
error_log logs/error.log error;
events {
use epoll; //多路复用
worker_connections 65535; //单个cpu处理的最大连接数
}

http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 128k;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host" '
'"$upstream_addr" "$upstream_status" "$request_time" "$upstream_cache_status" ';
access_log logs/access.log main;

client_max_body_size 64M;
sendfile on;
tcp_nopush on; //此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
tcp_nodelay on;

keepalive_timeout 30;
keepalive_requests 8192;
send_timeout 30; //两个连接活动之间的超时时间
reset_timedout_connection on;

gzip on;
gzip_min_length 10k;
gzip_buffers 4 16k;

gzip_disable "msie6";
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

open_file_cache max=102400 inactive=20s; //这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 30s; //这个是指多长时间检查一次缓存的有效信息
open_file_cache_min_uses 2; //open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
open_file_cache_errors on;

 


proxy_temp_path /dev/shm/nginx_temp;
proxy_cache_path /dev/shm/nginx_cache levels=1:2 keys_zone=cache_woyaopao:200m inactive=1d max_size=1g; //设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。

proxy_connect_timeout 10; //后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_read_timeout 180; //连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
proxy_send_timeout 5; //后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_buffer_size 16k; //设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
proxy_buffers 4 64k; //设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 256k;


include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

upstream baoming.woyaopao.com{
#ip_hash;
server 127.0.0.1:8201 max_fails=2 fail_timeout=600s ;
server 127.0.0.1:8202 max_fails=2 fail_timeout=600s ;

}

server {
listen 80;
server_name baoming.woyaopao.com;
add_header Access-Control-Allow-Origin baoming.woyaopao.com;
access_log logs/host.access.log main;
location /index.html {
return 301
$scheme://baoming.woyaopao.com/woyaopao/webpages/index/index.html;
}
location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$ {
proxy_pass http://baoming.woyaopao.com;
proxy_cache cache_woyaopao;
proxy_cache_valid 200 304 302 24h;
add_header X-Cache $upstream_cache_status;
}

location /woyaopao/ {
proxy_pass http://baoming.woyaopao.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect default;
add_header X-Cache $upstream_cache_status;
}
location ^~ /woyaopao/.*\.mp {
expires -1; //缓存永久过期,数字代表时间
proxy_pass http://baoming.woyaopao.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect default;
add_header X-Cache $upstream_cache_status;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

Nginx启动,关闭,平滑重启

检查nginx配置文件合法性。
/nginxpath/nginx -t -c /nginxconfpath/nginx.conf
启动nginx
/nginxpath/nginx -c /nginxconfpath/nginx.conf

Linux进程信号:
QUIT 处理完当前请求关闭进程 一般用于stop
HUP 重新加载配置 一般用于reload
USR1 日志切换
USR2 平滑升级
WINCH 从容关闭

推荐阅读