1.nginx 安装
1).解压文件
# tar zxf nginx-1.12.0.tar.gz
2).创建用户
# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx
3).修改配置文件
# vim nginx-1.12.0/auto/cc/gcc
172 #CFLAGS="$CFLAGS -g" (注释掉这行,去掉 debug 模式编译,编译以后程序只有几百 k)
# vim nginx-1.12.0/src/core/nginx.h
12 #define nginx_version 1012000
13 #define NGINX_VERSION "1.12.0"
14 #define NGINX_VER "nginx" (修改此行, 去掉后面的 “ NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号)
4).安装软件包依赖文件和nginx服务
# yum install gcc pcre-devel openssl-devel -y
# ./configure --prefix=/usr/local/lnmp/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module
5).重新编译
# make && make install
6).做软链接
# ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/
# nginx 启动nginx
# nginx -s stop 停止服务
测试:
[root@server1 sbin]# nginx
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 20 Jul 2017 13:42:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 20 Jul 2017 12:57:37 GMT
Connection: keep-alive
ETag: "5970a8c1-264"
Accept-Ranges: bytes
2.nginx 进程数设置 ,处理最大连接数设置
Nginx默认没有开启利用多核CPU,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU。CPU是任务处理,计算最关键的资源,CPU核越多,性能就越好。
通过 cat /proc/cpuinfo或lscpu来看cpu核心数
(1)cpu有多少个核,就有几位数,1代表内核开启,0代表内核关闭
(2)worker_processes最多开启8个,8个以上性能就不会再提升了,而且稳定性会变的更低,因此8个进程够用了
配置Nginx多核CPU,worker_cpu_affinity使用方法和范例
1. 2核CPU,开启2个进程
# vim /usr/local/lnmp/nginx/conf/nginx.conf
3 worker_processes 2; #2个进程
4 worker_cpu_affinity 01 10; #cpu内核1和2
13 events {
14 worker_connections 65535; #连接数
15 }
118 server {
119 listen 80;
120 server_name www.westos.org;
121 location / {
122 root /web1;
123 index index.html;
124 }
# vim /etc/security/limits.conf
51 nginx - nofile 65535
# usermod -s /bin/bash nginx
切换到nginx执行ulimit -a进行查看最大连接数
nginx -t#检测语法
nginx#运行 nginx
nginx -s reload#重载主配置文件
nginx -s stop#关闭 nginx
# ulimit -a
# mkdir /web1
# cat /web1/index.html
<h1>www.westos.org</h1>
# nginx -s reload
测试:
##nginx的https加密服务
# vim /usr/local/lnmp/nginx/conf/nginx.conf
99 server {
100 listen 443 ssl;
101 server_name localhost;
102
103 ssl_certificate cert.pem;
104 ssl_certificate_key cert.pem;
105
106 ssl_session_cache shared:SSL:1m;
107 ssl_session_timeout 5m;
108
109 ssl_ciphers HIGH:!aNULL:!MD5;
110 ssl_prefer_server_ciphers on;
111
112 location / {
113 root html;
114 index index.html index.htm;
115 }
116 }
## 生成数字证书
# cd /etc/pki/tls/private/
# openssl genrsa 2048 > localhost.key
# cd /etc/pki/tls/certs/
# make testcert
# cd /etc/pki/tls/certs/
# make cert.pem
# mv /etc/pki/tls/certs/cert.pem /usr/local/lnmp/nginx/conf/
# nginx -t
# nginx -s reload
# netstat -antlp 查看端口
测试:
###### nginx 访问控制######
# vim /usr/local/lnmp/nginx/conf/nginx.conf
49 location /status {
50 stub_status on;
51 access_log off;
52 allow 172.25.62.250; ##只允许172.25.62.250访问
53 deny all;
54 }
# nginx -t
# nginx -s reload
测试:# curl http://172.25.62.1/status
######## nginx网页重写 ######
访问www.westos.org跳转到https://www.westos.org
# vim /usr/local/lnmp/nginx/conf/nginx.conf
105 server {
106 listen 443 ssl;
107 server_name www.westos.org;
108
109 ssl_certificate cert.pem;
110 ssl_certificate_key cert.pem;
111
112 ssl_session_cache shared:SSL:1m;
113 ssl_session_timeout 5m;
114
115 ssl_ciphers HIGH:!aNULL:!MD5;
116 ssl_prefer_server_ciphers on;
117
118 location / {
119 root /wed1;
120 index index.html index.htm;
121 }
122 }
123
124 server {
125 listen 80;
126 server_name www.westos.org;
127
128 rewrite ^(.*)$ https://www.westos.org$1 permanent;
129 }
$1可以让访问指定目录,permanent 永久 redirect 暂时
# nginx -t
# nginx -s reload
测试:
######## nginx负载均衡 #######
1.轮询(默认weight=1)默认选项,当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2.weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如果后端服务器down掉,能自动剔除。
比如下面配置,则1.11服务器的访问量为1.10服务器的两倍。
3.ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session不能跨服务器的问题。如果后端服务器down掉,要手工down掉。
4.当2,3的服务都down掉后,本地的服务就会顶上,显示信息proxy_pass反向代理每次改完配置文件都要nginx -t 进行语法检查,nginx -s reload进行路径更新nginx -s stop关闭服务
# vim /usr/local/lnmp/nginx/conf/nginx.conf
18 http {
19 upstream westos {
20 ip_hash;
21 server 172.25.62.2:80 weight=2;
22 server 172.25.62.3:8080;
23# server 172.25.62.1:8000 backup;
24 }
25 include mime.types;
26 default_type application/octet-stream;
128 server {
129 listen 80;
130 server_name www.westos.org;
131
132 # rewrite ^(.*)$ https://www.westos.org$1 permanent;
133 location / {
134 proxy_pass http://westos;
135 }
136 }
测试:
# for i in {1..10};do curl www.westos.org;done
<h1>server3-www.westos.org</h1>
<h1>server2</h1>
<h1>server3-www.westos.org</h1>
<h1>server2</h1>
<h1>server3-www.westos.org</h1>
<h1>server2</h1>
<h1>server3-www.westos.org</h1>
<h1>server2</h1>
<h1>server3-www.westos.org</h1>
<h1>server2</h1>
本文转自cuijb0221 51CTO博客,原文链接:http://blog.51cto.com/cuijb/1949919