nginx摘录

nginx摘录

一、nginx安装和配置

1, nginx windows安装启动
cd d:\nginx
start nginx
对nginx进程控制可以
nginx -s [ stop | quit | reopen | reload ]

2, nginx LINUX 安装
./configure
make
make install

Nginx默认安装到/usr/local/nginx目录下.可以通过./configure --help看编译选项

3, nginx 启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-c 指定了配置文件的路径

4, nginx停止
从容停止:kill -QUIT `/usr/local/webserver/nginx/logs/nginx.pid`
快速停止:kill -TERM `/usr/local/webserver/nginx/logs/nginx.pid` 或者 kill -INT `/usr/local/webserver/nginx/logs/nginx.pid`
强制停止:pkill -9 nginx

5, nginx平滑重启
检测配置文件是否正确: nginx -t
kill -HUP `/usr/local/webserver/nginx/logs/nginx.pid`

6, nginx 的信号控制
TERM,INT 快速关闭
QUIT 从容关闭
HUP 平滑重启,重新加载配置文件
USR1 重新打开日志文件,在切割日志时用途很大
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

7, nginx的平滑升级(不中断服务)
* 将新版本编译安装到旧版本的Nginx安装路径中。之前,最好备份旧的可执行文件
* kill -USR2 旧版本的Nginx进程ID
* 旧版本nginx的主进程将命名他的pid文件为.oldbin(/usr/local/webserver/nginx/nginx.pid.oldbin),然后执行新版本的nginx可执行程序
* 这时,新的旧的都在运行,kill -WINCH 旧的版本进程 使得从容关闭
* 慢慢的新的版本开始工作
* 还可以选择恢复用旧的版本:
kill -HUP 旧的主进程号:Nginx将在不重新载入配置文件的情况下启动工作进程
kill -QUIT 新的主进程号:从容关闭其他工作进程
kill -TERM 新的主进程号:强制退出


二、Nginx的配置与优化

1, nginx完整配置示例

#运行用户和组
user www www;

#指定工作衍生进程数(一般等于CUP总核或者总核数的两倍)
worker_processes 8;

#全局错误日志及PID文档 [debug | info | notice | warn | error | crit]
error_log logs/error.log notice;
pid logs/Nginx.pid;

#指定文件描述符数量
worker_rlimit_nofile 51200;

#工作模式及连接数上限
events {
# 使用的网络I/O模型,linux推荐epool;freebsd推荐kquequ模型
use epoll;
#允许的链接数
worker_connections 1024;
}

#设定http服务器,利用他的反向代理功能提供负载均衡支持
http
{
# 开两 php-cgi 服务,端口连接方式速度快,socket方式稳定
# 使用 lighttpd 的 spawn-fcgi 起的fast-cgi
# weight 是设置权重
upstream phpfastcgi {
server unix:/tmp/php-fastcgi0.sock weight=1;
server unix:/tmp/php-fastcgi1.sock weight=1;
# server 127.0.0.1:8000 weight=1;
# server 127.0.0.1:8001 weight=1;
}

# mime 类型 和 默认 header-type
include mime.types;
default_type application/octet-stream;

# 默认 header-charset
charset utf-8;

# 一些限制
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

# sendfile 应该是 lighttpd 的 sendfile 是一个意思
sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

# fastcgi 配置
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

# 开启gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

# 定义日志格式
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';

# 定义一个虚拟机
server
{
# 监听端口
listen 80;
# 虚拟机名
server_name klpt-test.domain.com;
# 如打开的是一个目录,默认的搜 索文件的顺序
index index.html index.htm index.php;
# 虚拟机指向的路径
root /data/www/klpt-test.domain.com/webroot;

# 如果访问的路径不存在,那么 rewrite给根目录的 index.php,路径以参数url来传递
location / {
index index.html index.php;

if (-f $request_filename) {
break;
}

if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}

# 配置PHP
location ~ \.php$ {
fastcgi_pass phpfastcgi;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/klpt-test.domain.com/webroot$fastcgi_script_name;
include fastcgi_params;
}

# 图片缓存 30 天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}

# js 和 css 缓存 1 小时
location ~ .*\.(js|css)$ {
expires 1h;
}
}

server
{
# 定义的虚拟机监听端口是 443
listen 443;
server_name klpt.domain.com;
index index.html index.htm index.php;
root /data/www/klpt.domain.com/webroot;

# 开启 ssl 服务
# 命令 openssl req -new -x509 -nodes -out klpt-sqladmin.crt -keyout klpt-sqladmin.key
ssl on;
ssl_certificate /data/etc/nginx7/conf/klpt-sqladmin.crt;
ssl_certificate_key /data/etc/nginx7/conf/klpt-sqladmin.key;
ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

#limit_conn crawler 20;
# 如果访问的路径不存在,那么rewrite给根目录的 index.php,路径以参数url来传递
location / {
index index.html index.php;

if (-f $request_filename) {
break;
}

if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}

# php config
location ~ \.php$ {
fastcgi_pass phpfastcgi;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/klpt.domain.com/webroot$fastcgi_script_name;
# 开启 https ,需要此配置
fastcgi_param HTTPS on;
include fastcgi_params;
}

# 将静态文件缓存 30 天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
expires 30d;
}

# log
access_log /data/log/nginx/nginx_access/nginx_klpt_access.log access;
}

# 静态服
server
{
listen 80;
server_name klpt-static.domain.com;
index index.html index.htm;
root /data/www/klpt-static.domain.com;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
expires 30d;
}
}

server
{
listen 443;
server_name klpt-sqladmin.domain.com;
index index.html index.htm index.php;
root /data/www/klpt-sqladmin.domain.com;

ssl on;
ssl_certificate /data/etc/nginx7/conf/klpt-sqladmin.crt;
ssl_certificate_key /data/etc/nginx7/conf/klpt-sqladmin.key;

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

location ~ \.php$ {
fastcgi_pass phpfastcgi;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/klpt-sqladmin.domain.com$fastcgi_script_name;
# 开启 https ,需要此配置
fastcgi_param HTTPS on;
include fastcgi_params;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
expires 30d;
}

access_log /data/log/nginx/nginx_access/nginx_sqladmin_access.log access;
}
}

2, nginx虚拟机配置

一个简化的例子
http
{
server
{
listen 80 default;
server_name _ *;
access_log logs/default.access.log combined;
location / {
index index.html;
root /data0/htdocs/htdocs;
}
}
}

配置基于IP的虚拟主机

给eth0网卡设备添加两个IP别名192.168.8.43 和192.168.8.44
ifconfig eth0:1 192.168.8.43 netmask 255.255.255.0 up
route add -host 192.168.8.43 dev eth0:1

ifconfig eth0:2 192.168.8.44 netmask 255.255.255.0 up
route add -host 192.168.8.44 dev eth0:2

配置
http
{
#第一个虚拟主机
server
{
listen 192.168.8.43:80;
server_name 192.168.8.43;
access_log logs/server1.access.log combined;
location / {
index index.html;
root /data0/htdocs/server1;
}
}
#第二个虚拟主机
server
{
listen 192.168.8.44:80;
server_name 192.168.8.44;
access_log logs/server2.access.log combined;
location / {
index index.html;
root /data0/htdocs/server2;
}
}
#第三个虚拟主机
server
{
listen 192.168.8.45:80;
server_name 192.168.8.45;
access_log logs/server3.access.log combined;
location / {
index index.html;
root /data0/htdocs/server3;
}
}
}

配置基于域名的虚拟主机
aaa.domain.com 第一个虚拟主机处理
bbb.otherdomain.com 第二个虚拟主机处理
第三个处理除了aaa.domain.com之外的*.domain.com和www.domain.com domain.com

http
{
#11111111
server
{
listen 80 default;
server_name aaa.domain.com;
access_log logs/aaa.domain.com.access.log combined;
location / {
index index.html;
root /data0/htdocs/aaa.domain.com;
}
}
#2222222
server
{
listen 80 default;
server_name bbb.otherdomain.com;
access_log logs/aaa.domain.com.access.log combined;
location / {
index index.html;
root /data0/htdocs/bbb.otherdomain.com;
}
}
#33333
server
{
listen 80 default;
server_name www.domain.com domain.com *.domain.com;
access_log logs/domain.com.access.log combined;
location / {
index index.html;
root /data0/htdocs/domain.com;
}
}
}

3, nginx的日志文件配置与切割

log_format main '$remote_addr – $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" ' ;
记录真实IP
log_format main '$http_x_forwarded_for – $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" ' ;

日志分割
mv /data1/logs/access.log /data1/logs/20090318.log
kill -USR1 nginx进程号

写成脚本
#!/bin/bash
logs_path = "/data1/logs/"

mkdir -p ${logs_path}${date -d "yesterday" + "%Y"}/{date -d "yesterday" + "%m"}/
mv ${logs_path}/access.log ${logs_path}${date -d "yesterday" + "%Y"}/{date -d "yesterday" + "%m"}/access_${date -d "yesterday" + "%Y%m%d"}.log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

crontab -e
00 00 * * * /bin/bash the.sh


三、Nginx反向代理和负载均衡
nginx的upstream目前支持4种方式的分配

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2)、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2)、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3)、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4)、url_hash(第三方)

upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态

server 127.0.0.1:9090 down;

server 127.0.0.1:8080 weight=2;

server 127.0.0.1:6060;

server 127.0.0.1:7070 backup;

}
在需要使用负载均衡的server中增加代码
proxy_pass [url]http://bbs.linuxtone.org/[/url];

每个设备的状态设置为:代码:
1.down 表示单前的server暂时不参与负载

2.weight 默认为1.weight越大,负载的权重就越大。

3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

4.fail_timeout:max_fails次失败后,暂停的时间。

5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug

client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡


代码实例

……….

#loadblance my.linuxtone.org

upstream my.linuxtone.org {

ip_hash;

server 127.0.0.1:8080;

server 192.168.169.136:8080;

server 219.101.75.138:8080;

server 192.168.169.117;

server 192.168.169.118;

server 192.168.169.119;

}

…………..

include vhosts/linuxtone_lb.conf;

………

#vi proxy.conf

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;

client_max_body_size 50m;

client_body_buffer_size 256k;

proxy_connect_timeout 30;

proxy_send_timeout 30;

proxy_read_timeout 60;



proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

proxy_max_temp_file_size 128m;

proxy_store on;

proxy_store_access user:rw group:rw all:r;

#nginx cache

client_body_temp_path /data/nginx_cache/client_body 1 2;

proxy_temp_path /data/nginx_cache/proxy_temp 1 2;#vi linuxtone_lb.conf

server

{
listen 80;
server_name my.linuxtone.org;
index index.php;
root /data/www/wwwroot/mylinuxtone;
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php break;
}

error_page 403 [url]http://my.linuxtone.org/member.php?m=user&a=login[/url];
location / {
if ( !-e $request_filename) {
proxy_pass [url]http://my.linuxtone.org[/url];
break;
}
include /usr/local/nginx/conf/proxy.conf;
}
}


四、Nginx Redirect

将所有linuxtone.org与abc.linuxtone.org域名全部自跳转到http://www.linuxtone.org代码

server
{
listen 80;
server_name linuxtone.org abc.linuxtone.org;
index index.html index.php;
root /data/www/wwwroot;
if ($http_host !~ "^www\.linxtone\.org$") {
rewrite ^(.*) [url]http://www.linuxtone.org[/url]$1 redirect;
}
}

 

 

你可能感兴趣的:(nginx)