以下所有操作都是在Red-hat 6.5上
1.安装nginx
安装前需要做的事情:
先创建nginx用户–>useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx (全球nginx的uid都是800)
再安装之前,先将/nginx-1.12.0/src/core/nginx.h中
14 #define NGINX_VER “nginx/” ##删除版本号,安全一点!
vim /nginx-1.12.0/auto/cc/gcc
172 #CFLAGS=”$CFLAGS -g” ##注释掉这行,忽略编译信息,安装的容量会变小。
yum install -y pcre-devel #正则表达式语法库 (解决依赖性)
yum install -y openssl-devel #安装ssl库
./configure --prefix=/usr/local/lnmp/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module #创建makefile文件并且检查makefile是否正确
--prefix=/usr/local/lnmp/nginx #安装路径
--user=nginx --group=nginx #指定用户和用户组名称
--with-threads #开启多线程
--with-file-aio #异步io
--with-http_ssl_module #开启ssl模块
--with-http_stub_status_module #开启监控模块
make && make install #make根据makefile的内容编译出符合平台的可执行文件
#make install 安装编译成功的软件
提示:开启nginx,直接进入/usr/local/lnmp/nginx/sbin执行 ./nginx即可打开,如果遇到说端口占用,就要关掉80端口的程序
2.Nginx的进程优化
2.
设置nginx连接数和最大进程数
3 worker_processes 2; #最大进程数
4
5 worker_cpu_affinity 01 10; #(worker与cpu物理核心绑定,减少cpu上下文切换的消耗;)
14 events {
15 worker_connections 65535; #最大连接到nginx的clients数目
16 }
vim /etc/security/limits.conf #在此文件下设置才会生效
末尾添加
添加
nginx - nofile 65535
3.添加虚拟主机
vim /usr/local/lnmp/nginx/conf/nginx.conf
118 server {
119 listen 80;
120 server_name www.westos.org; ##域名
121
122 location / {
123 root /web1; ##默认发布目录
124 index index.html; ##发布文件
125 }
126
127 }
然后在创建/web1目录,在目录里面创建index.html文件
4.证书认证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 }
证书和密钥的制作:
cd /etc/pki/CA/private/
openssl genrsa 2048 > localhost.key #生成加密文件(密钥)
cd /etc/pki/tls/certs/
make cert.pem #制作证书
然后填写所需信息
mv cert.pem /usr/local/lnmp/nginx/conf/ #因为nginx.conf中.pem写的绝对路径,要写到当前目录下
nginx -t #检验语言是否正确
nginx -s reload #刷新(并不是重启)
5.监控网页连接数:(不允许别人来查看,只能自己查看)
vim /usr/local/lnmp/nginx/conf/nginx.conf
49 location /status {
50 stub_status on;
51 access_log off;
52 allow 172.25.77.1;
53 deny all; #不让任何人查看除过allow的ip
54 }
检测的时候:在网页上写入172.25.41.1/status
6.网站重写:(输入www.westos.org 转入 https://www.westos.org)
132 server {
133 listen 80;
134 server_name www.westos.org;
141 rewrite ^(.*)$ https://www.westos.org$1 permanent;
} $1这里指目录下可以指定别的文件 permanent指永久的!
^(.*)$ 正则表达式,指所有文件。
7.负载均衡+反向代理:
18 http {
19 upstream westos { #upstream 负载均衡模块
20 ip_hash; #同一个ip访问一个后端
21 server 172.25.41.2:80 weight=2;
22 server 172.25.41.3:8080;
23 #server 127.0.0.1:8000 backup; #主备模式,如果其他两个都挂了,就会出现,显示里面的内容!
24 }
146 location / {
147 proxy_pass http://westos; #代理头
148 }