Linux(centos) nginx安装和nginx入门配置方法
1.切换至root用户
$su -
2.执行在线安装命令
#yum install nginx -y
如果上面提示以下内容
[root@template ~]# yum install nginx -y
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
No package nginx available.
Error: Nothing to do
[root@template ~]# yum install nginx -y
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
No package nginx available.
则需要配置nginx的源,如果直接安装则忽略这里的配置源步骤。
配置yum的nginx源/etc/yum.repos.d/
目录下创建一个源配置文件nginx.repo
命令:
cd /etc/yum.repos.d/
vim nginx.repo
nginx.repo内容输入:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
一般情况centos7会自带源的,只有部分centos6没有。所以配置源。配置完成后再执行安装命令
yum install nginx -y
3.安装完成,启动nginx命令
#nginx
4.启动后在浏览器输入刚才安装好nginx的服务器IP地址
出现上面的页面标识安装和启动成功
5.nginx停止
#nginx -s stop
6.nginx通用配置文件nginx.conf配置说明
#指定nginx进程运行用户及用户组(默认nginx)(动静分离时候需要。注意权限否则静态文件可能访问不到。)
user nginx;
#nginx要开启的进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数
#推荐最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了
worker_processes 8;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#worker_processes 4;#4个进程配置
#worker_cpu_affinity 0001 0010 0100 1000
error_log /var/log/nginx/error.log warn;#错误日志存放路径,以及日志级别默认warn
pid /var/run/nginx.pid;#默认存放nginx进程id文件
#当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致
#[注]文件资源限制的配置可以在/etc/security/limits.conf设置,针对root/user等各个用户或者*代表所有用户来设置。
worker_rlimit_nofile 65535;
events {
#使用epoll的I/O模型,用这个模型来高效处理异步事件,epoll是linux平台下的高效模式
use epoll;
#每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections
#定义nginx每个进程的最大连接数为51200,一般网上都配置65535,根据张宴大神的建议51200即可
worker_connections 51200;#默认1024
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃!
keepalive_timeout 65;
#客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
client_header_buffer_size 4K;
#############下面部分为动静分离需要nginx代理文件时候使用,仅做Tomcat分发则不需要启用############
#下面这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
#open_file_cache max=102400 inactive=20s;
#下面这个是指多长时间检查一次缓存的有效信息。
#open_file_cache_valid 30s;
#open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
#open_file_cache_min_uses 1;
############上面面部分为动静分离需要nginx代理文件时候使用,仅做Tomcat分发则不需要启用############
#隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。
server_tokens off;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
7.新增一个nginx解析
切换目录至配置目录
#cd /etc/nginx/conf.d/
新建一个解析配置文件test.conf
#server {###配置虚拟机
# listen 80;#配置监听端口
# server_name zhljc.com www.zhljc.com srv.gift www.srv.gift 127.0.0.1; #配置访问域名,可以有多个[127.0.0.1/localhost表示任何]
# location / {#对所有地址进行负载均衡
# root html;##定义服务器的默认网站根目录位置
# index index.html index.htm;##定义首页索引文件的名称
#
# #以下三行,目的是将代理服务器收到的用户的信息传到真实服务器上
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#
# #websocket
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
#
# proxy_pass http://localhost:8080/bamboo;
# }#end location
# if ($host = 'www.xqlee.net'){
# rewrite ^/(.*)$ http://xqlee.net/$1 permanent;
# }
#
#}#end server
#upstream tomcats-webservice {#设定负载均衡的服务器列表,注意多个负载时候这里的名字必须唯一
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
#负载方式,默认
#1.轮询[每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除]
#2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
#3.ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#server localhost:8000 weight=1 max_fails=3 fail_timeout=60s; #第一台机器
#server localhost:8080 weight=1 max_fails=3 fail_timeout=60s; #第二台机器
#ip_hash;
#server localhost:8000;
#}#end upstream
http://blog.xqlee.com/article/48.html