Nginx服务配置
在虚拟控制台把vm1的Memory改为2048,启动vm1
lftp下载nginx-1.12.0.tar.gz
tar zxf nginx-1.12.0.tar.gz
useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx
yum isntall gcc pcre-devel openssl-devel -y
cd /root/nginx-1.12.0/src/core
vim nginx.h
改为#define NGINX_VER "nginx"
cd /root/nginx-1.12.0/atuo/cc/
vim gcc
注释掉dubug
cd /root/nginx-1.12.0
./configure --prefix=/usr/local/lnmp/nginx --user=nginx --group=nginx--with-threads --with-file-aio --with-http_ssl_module--with-http_stub_status_module
make && make install
ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin
nginx
nginx -s reload
进行检测curl -I localhost
vim /usr/local/lnmp/nginx/conf/nginx.conf
Nginx默认没有开启利用多核CPU,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU。CPU是任务处理,计算最关键的资源,CPU核越多,性能就越好。
通过 cat /proc/cpuinfo来看cpu核心数
规则设定
(1)cpu有多少个核,就有几位数,1代表内核开启,0代表内核关闭
(2)worker_processes最多开启8个,8个以上性能就不会再提升了,而且稳定性会变的更低,因此8个进程够用了
配置Nginx多核CPU,worker_cpu_affinity使用方法和范例
1. 2核CPU,开启2个进程
worker_processes 2;
worker_cpu_affinity 01 10;
01表示启用第一个CPU内核,10表示启用第二个CPU内核
worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。
2. 2核CPU,开启4个进程
worker_processes 4;
worker_cpu_affinity 01 10 01 10;
开启了四个进程,它们分别对应着开启2个CPU内核
worker_rlimit_nofile65535;
这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文
件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
现在在linux2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
查看Linux系统文件描述符的方法:
[root@web001 ~]# sysctl -a | grep fs.file
Vim /etc/security/limits.conf
nginx - nofile 65535
Usermod -s /bin/bash nginx
切换到nginx执行ulimit -a进行查看
vim /usr/local/lnmp/nginx/conf/nginx.conf
在最后面加入
server {
listen80;
server_name www.westos.org;
location / {
root /web1;
index index.html;
}
nginx -t 进行配置文件语法检查
nginx -s reload
mkdir /web1
echo www.westos.org > /web1/index.html
访问172.25.22.1进行测试
vim /usr/local/lnmp/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
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;
}
}
cd /etc/pki/tls/private/
Openssl genrsa 2048 > localost.key
cd /etc/pki/tls/certs
make testcert
make cert.pem
mv cert.pem /usr/local/lnmp/nginx/conf/
nginx -t
nginx -s reload
netstat -antlp 端口查看
访问https://172.25.22.1测试
vim /usr/local/lnmp/nginx/conf/nginx.conf
访问控制
location /status {
stub_status on;
access_log off;
allow 172.25.254.22;
deny all;
}
通过curl localhost/status进行测试查看
访问重写,访问www.westos.org跳转到https://www.westos.org
server {
listen80;
server_name www.westos.org;
rewrite^(.*)$ https://www.westos.org$1 permanent;
}
}
$1可以让访问指定目录,permanent 永久 redirect 暂时
nginx的轮询机制
打开vm2,vm3,vm3httpd的端口改为8080 vm1的httpd端口改为8000对httpd服务进行重启
http {
upstreamwestos {
server172.25.0.2:80;
server172.25.0.3.8080;
Server127.0.0.1:8000 backup;
}
注释rewrite
加上location / {
proxy_pass http://westos;
}
1、轮询(默认weight=1)
默认选项,当weight不指定时,各服务器weight相同,
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream westos {
server 172.25.22.2:80;
server 172.25.22.3:8080;
}
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
如果后端服务器down掉,能自动剔除。
比如下面配置,则1.11服务器的访问量为1.10服务器的两倍。
upstream bakend {
server 172.25.22.2 weight=1;
server 172.25.22.3 weight=2;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session不能跨服务器的问题。
如果后端服务器down掉,要手工down掉。
upstream resinserver{
ip_hash;
server 172.25.22.2:80;
server 172.25.22.3:8080;
}
当2,3的服务都down掉后,本地的服务就会顶上,显示信息
proxy_pass反向代理
每次改完配置文件都要nginx -t 进行语法检查,nginx -s reload进行路径更新
nginx -s stop关闭服务
2502

被折叠的 条评论
为什么被折叠?



