Nginx配置
# 运行用户 # user nobody; # 启动进程 通常设置成和CPU的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; # PID => process ID #pid logs/nginx.pid; # 工作模式及链接数上线 events { # epoll是多路复用IO(I/O Multiplexing)中的一种方式 # 仅用于linux2.6以上内核,可以大大提高nginx的性能 # use epoll # 单个后台worker process 进程的最大并发链接数 worker_connections 1024; # 并发总数是worker_process 和 woker_connections 的乘积 # 即max_clients = worker_process * woker_connections # 在设置了反向代理的情况下,max_clients = worker_process * woker_connections / 4 为什么呢? # 为什么上面的反向代理要除以4,应该说是一个经验值 # 根据以上条件,正常情况下的Nginx Server可以应付的最大链接数为 4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关 # 因为并发手IO的约束,max_clients的值须小于系统可以打开的最大文件数 # 我们来看看360M内存的VPS可以打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max # 输出34336 # 32000 < 34336, 即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统的承受范围之内 # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当的进行设置 # 适合并发总数小于操作系统可以打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置 # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源 # ulimit -SHn 65535 # ulimit 即设置当前shell以及由它启动的进程的资源限制。显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题 } http { # 设定 mime类型,类型有mime.type文件定义 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 指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件 # 对于普通应用,必须设为on # 如果用来进行下载等应用磁盘IO重负载应用可设置为off # 以平衡磁盘与网络I/O处理速度,降低系统的uptime sendfile on; #tcp_nopush on; # 链接超时时间 #keepalive_timeout 0; keepalive_timeout 65; # 开起gzip压缩 #gzip on; #gzip_disable "MSIE [1-6]." # 设定请求缓冲 # client_header_buffer_size 128k; # large_client_header_buffers 4 128k; # 设定虚拟主机配置 server { # 侦听8080端口 listen 8080; #定义使用 localhost 访问 server_name localhost; # 定义服务器的默认网站根目录位置 # root html; #charset koi8-r; # 设定本虚拟主机的访问日志 #access_log logs/host.access.log main; # 默认请求 location / { root html; # 定义首页索引文件的名称 index index.html index.htm; } #定义错误提示页面 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # 静态文件,nginx自己处理 # location ~ ^/(images|javascript|js|css|flash|media|static)/ { # 过期三十天,静态文件不怎么更新,过期可以设大一点 # 如果频繁更新,则可以设置得小一点 # expires 30d; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # # 禁止访问 .htxxx文件 #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*; }
运行和控制Nginx
Nginx命令行参数
- -c </path/to/config> 为Nginx指定一个配置文件,来代替缺省的
- -t 不运行,仅仅测试配置文件,nginx将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件
- -v 显示nginx的版本
- -V 显示nginx的版本,编译器版本和参数配置
nginx控制信号
可以使用信号系统来控制主进程。默认,nginx 将其主进程的 pid 写入到 /usr/local/nginx/nginx.pid 文件中。通过传递参数给 ./configure 或使用 pid 指令,来改变该文件的位置。
主进程可以处理以下的信号:
TERM, INT | 快速关闭 |
QUIT | 从容关闭 |
HUP | 重载配置 用新的配置开始新的工作进程 从容关闭旧的工作进程 |
USR1 | 重新打开日志文件 |
USR2 | 平滑升级可执行程序。 |
WINCH | 从容关闭工作进程 |
尽管你不必自己操作工作进程,但是,它们也支持一些信号:
TERM, INT | 快速关闭 |
QUIT | 从容关闭 |
USR1 | 重新打开日志文件 |
nginx启动、停止、重启命令
nginx启动
sudo /usr/local/nginx/nginx
nginx从容停止命令,立刻关闭nginx进程
ps -ef lgrep nginx
Kill -TERM nginx 主进程号
如果以上命令不管用,可以强制停止
kill -9 nginx 主进程号
如果嫌麻烦,可以不用查看进程号,直接使用命令进行操作
其中/usr/local/nginx/nginx.pid为nginx.conf中pid命令设置的参数用来存放nginx的主进程号的文件
kill -信号类型 cat /usr/local/nginx/nginx.pid
# 例如 kill -QUIT `cat /usr/loacl/nginx/nginx.pid` # 启动 nginx # 停止 nginx -s stop
nginx.conf 配置文件目录
nginx -t