centos下nginx的安装与配置

nginx安装与配置笔录

@(环境)[centos6.5]

[TOC]

安装

到nginx官网下载对应版本的nginx,地址http://nginx.org/en/download.html
我这里下载的是nginx-1.8.1.tar.gz
上传至服务器

1
scp nginx-1.8.1.tar.gz root@192.168.0.112:/home

登录服务器并进入home目录
首先先安装必要的安装的环境,执行如下命令

1
2
3
yum install gcc gcc-c++ -y
yum install pcre.x86_64 pcre-devel.x86_64 -y
yum install zlib-devel.x86_64 zlib.x86_64 -y

解压

1
tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/

设置配置

1
2
cd /usr/local/nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-pcre --user=daemon --group=daemon --with-http_stub_status_module

编译与安装

1
make && make install

此时nginx将会安装到/usr/local/nginx中
建立软连接,以便在任何目录下都能执行nginx命令

1
ln -s /usr/local/nginx/sbin/nginx   /usr/local/sbin/

配置

编辑配置

1
vim nginx.conf

改为如下内容

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
#user  nobody;
worker_processes 1;

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

pid logs/nginx.pid;


events {
use epoll;
worker_connections 1024;
}


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;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k; #缓冲区代理缓冲用户端请求的最大字节数,
large_client_header_buffers 4 32k;
client_max_body_size 300m; #允许客户端请求的最大单文件字节数

tcp_nodelay on;
client_body_buffer_size 512k;

gzip on;
gzip_min_length 1k; #需要压缩的最小长度
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server_name_in_redirect off;

server {
listen 80 default_server;
server_name _;
location /
{
proxy_pass http://test-nginx.com;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
client_max_body_size 100m;
}
error_page 403 404 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /usr/local/nginx/conf/conf.d/fastcgi_pass.conf;
include /usr/local/nginx/conf/conf.d/proxy_pass.conf;
include vhost/*.conf;
}

添加其他配置

1
2
3
4
5
cd /usr/local/nginx/conf
mkdir conf.d
mkdir vhost
cd vhost
vim tomcat.conf

添加tomcat.conf内容如下

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
server {
listen 80;
server_name test-ng.com;

location ~ .*\.(gif|bmp|jpeg|jpg|png|css|js|html|htm|txt)$ {
proxy_pass http://test-nginx.com;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
proxy_pass http://test-nginx.com;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
client_max_body_size 100m;
}

location ~ ^/(WEB-INF)/ {
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream test-nginx.com {
server 192.168.0.110:8080 max_fails=3 fail_timeout=30s weight=4;
server 192.168.0.111:8080 max_fails=3 fail_timeout=30s weight=4;
}

192.168.0.110:8080和192.168.0.111:8080是两个运行在tomcat上的web服务
继续配置

1
2
cd ../conf.d
vim proxy_pass.conf

proxy_pass.conf内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#proxy_connect_timeout 300;
#proxy_read_timeout 300;
#proxy_send_timeout 200;

proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 64k;

proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#proxy_intercept_errors on|off default off;

继续配置

1
vim fastcgi_pass.conf

fastcgi_pass.conf内容如下

1
2
3
4
5
6
7
8
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 200s;
fastcgi_buffer_size 128k;
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;

完成

启动

1
nginx

停止

1
nginx -s stop

重新加载配置

1
nginx -s reload

热评文章