安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
准备目录
[root@VM_0_15_centos ~]# mkdir /usr/local/nginx
[root@VM_0_15_centos ~]# cd /usr/local/nginx
下载
[root@VM_0_15_centos nginx]# wget http://nginx.org/download/nginx-1.5.9.tar.gz
解压
[root@VM_0_15_centos nginx]# tar -zxvf nginx-1.5.9.tar.gz
解压好后移至目录
[root@VM_0_15_centos nginx]# cd nginx-1.5.9/
设置nginx安装目录,如果没有指定,默认为/usr/local/nginx
[root@VM_0_15_centos nginx-1.5.9]# ./configure --prefix=/usr/local/nginx
编译
make(把各种语言写的源码文件,变成可执行文件和各种库文件)
[root@VM_0_15_centos nginx-1.5.9]# make
安装
make install(把这些编译出来的可执行文件和库文件复制到合适的地方)
[root@VM_0_15_centos nginx-1.5.9]# make install
配置环境变量
不配置否则会提示找不到nginx命令(-bash: nginx: command not found)
1:进入 vim /etc/profile 文件
2:添加配置 如下
3:重启系统
注释:i插入,写完后esc键退出,:wq保存并退出
启动
[root@VM_0_15_centos nginx-1.5.9]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止
查询nginx主进程号
[root@VM_0_15_centos nginx-1.5.9]# ps -ef|grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
从容停止Nginx
kill -QUIT 主进程号
快速停止Nginx
kill -TERM 主进程号
强制停止Nginx
pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型 ‘/usr/nginx/logs/nginx.pid’
平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
平滑重启命令:
kill -HUP 主进程号或进程号文件路径
或者使用
/usr/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。
访问
在浏览器中输入IP:端口号(默认80),出现如下图所示,说明安装成功。
加了中文注释的nginx.conf
user www www;
# 工作进程个数,可配置多个
worker_processes auto;
error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
# 单个进程最大连接数
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
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;
fastcgi_intercept_errors on;
#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
######################## default ############################
# 服务器集群名称 和下面的location地址对应
upstream myServer {
# weigth参数表示权值,权值越高被分配到的几率越大
# server 127.0.0.1:8080 weight=1;
# server 127.0.0.1:8060 weight=1;
server 47.93.10.184:8080;
server 47.93.10.184:8081;
}
# 每一个server相当于一个代理服务器
server {
# 监听端口,默认80
listen 8848;
# 当前服务的域名,可以有多个,用空格分隔(我们是本地所以是localhost) www.kolbe.cn
server_name localhost;
#server_name _;
access_log /data/wwwlogs/access_nginx.log combined;
root /data/wwwroot/default;
# 当没有指定主页时,默认会选择这个指定的文件,可多个,空格分隔
index index.html index.htm index.php;
# 表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
location / {
# 请求转向自定义的服务器列表
proxy_pass http://myServer;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
########################## vhost #############################
include vhost/*.conf;
}