Nginx
来源:互联网 发布:mac 安装java环境 编辑:程序博客网 时间:2024/05/20 04:09
nginx
特点:高并发高吞吐,占有内存少。
安装
nginx-1.12.0.tar.gz
tar zxf nginx-1.12.0.tar.gz
nginx的简单初始配置
创建nginx用户,本地家目录,pid
useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx ##创建nginx的本地家目录,uid:80
配置nginx
cd ~/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 ##设置nginx的目录及各种模块比如openssl加密等,安装过程出现error。逐步安装所需要的devel开发包记得加-develmake && make install ##make成功后即可make install 安装vim ~/nginx-1.12.0/src/core/nginx.h14 #define NGINX_VER "nginx" vim ~/nginx-1.12.0/auto/cc/gcc172 #CFLAGS="$CFLAGS -g" ##关闭gcc的debug调试模式,节省空间/usr/local/lnmp/nginx/sbin/目录下:./nginx在同上目录下:ln -s /usr/local/lnmp/nginx/sbin/ /sbin/##方便调用
测试:
curl -I localhost
Nginx多核绑定
vim /usr/local/lnmp/nginx/conf/nginx.conf3 worker_processes 2; ##二核CPU,开启2个进程5 worker_cpu_affinity 01 10; ##01和10分别表示第一个CPU和第二个CPU15 worker_connections 65535; ##连接数,65535为最大的进程数usermod -s /bin/bash nginx ##添加nginx用户的交互模式vim /etc/security/limits.conf nginx - nofile 65535 ##最后一行添加,最大进程数
测试:
su - nginx
ulimit -a
Nginx虚拟主机
vim /usr/local/lnmp/nginx/conf/nginx.confserver { listen 80; ##通过80端口 server_name www.westos.org; 输入虚拟机域名 location / { ##location /,在/目录下 root /web1; ##虚拟地址 index index.html; ##虚拟主页}
测试:
curl -I www.westos.org
https,证书加密
配置nginx证书加密
vim /usr/local/lnmp/nginx/conf/nginx.confserver { listen 443 ssl; ##端口443,ssl加密端口 server_name www.westos.org; ssl_certificate cert.pem; ##pem,私钥 ssl_certificate_key cert.pem; ##pem??? ssl_session_cache shared:SSL:1m; ##缓存 ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /web1; index index.html index.htm; } }
生成证书与密钥
cd /etc/pki/tls/privateopenssl genrsa 2048 > localhost.key ##生成密钥localhost.keycd /etc/pki/tls/certsmake cert.pem ##生成证书,pem为私钥,在此目录下查看Makefile可知pem包含证书与加密mv cert.pem /usr/local/lnmp/nginx/conf/ ##移至nginx/conf目录下nginx -s reload ##重置nginx,使之生效
测试:
netstat -antlp ##查看443端口是否开启
访问https://www.westos.org
查看当前连接数与状态
vim /usr/local/lnmp/nginx/conf/nginx.conflocation /status { stub_status on; access_log off; allow 127.0.0.1; ##准许登录ip,若deny存在,则需写至deny前 deny all; ##拒绝登录
测试:
www.westos.org/status
curl localhost/status ##测试拒绝和允许时会显示链接状态
重写
- ####permanent永久跳转
1)vim /usr/local/lnmp/nginx/conf/nginx.confserver { listen 80; server_name www.westos.org; rewrite ^(.*)$ https://www.westos.org permanent; ##permanent:永久}
测试:
当输入www.westos.org时自动跳转至https://www.westos.org
curl -I www.westos.org
- redirect单次临时跳转
server { listen 80; server_name www.westos.org; rewrite ^(.*)$ https://www.westos.org redirect; ##redirect:重定向}
测试:
curl -I www.westos.org
负载均衡
- round-robin(轮询)
轮询是nginx中默认的负载均衡模式
vim /usr/local/lnmp/nginx/conf/nginx.confhttp { upstream westos { server 172.25.36.2:80 weight=2; ##轮询的主机,weight为权重,若不设置则默认为1 server 172.25.36.3:8080; }server { listen 80; server_name www.westos.org; location / { proxy_pass http://westos;}
将server2与server3的httpd的端口改为如上upstream中需求
测试轮询:
- 主备
http { upstream westos { server 172.25.36.2:80 weight=2; server 172.25.36.3:8080; server 127.0.0.1:8000 backup; ##当前两主机崩溃时由127.0.0.1接手}
测试主备(server2与server3关闭httpd服务,server1顶替):
- ip_hash
http { upstream westos { ip_hash; server 172.25.36.2:80; server 172.25.36.3:8080;}
ip_hash模式会根据来源IP和后端配置来做hash分配,确保固定IP只访问一个后端。
测试ip_hash(确保固定IP只访问一个后端):
阅读全文
0 0
- nginx
- Nginx
- Nginx
- Nginx
- Nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- Nginx
- nginx
- nginx
- nginx
- nginx
- 【Web开发的架构】
- 什么时候使用CountDownLatch
- web前端综合性总结
- SpringMVC学习(三)映射请求参数&请求参数
- HTTP cookies 讲解(HTTP cookies explained )
- Nginx
- Poj 1985 Cow Marathon ( 树的直径
- kNN在CIFAR10上的应用
- 一小时包教会 —— webpack 入门指南
- Hibernate Cache
- Android6.0及以上动态申请权限详细讲解
- 未来の夢
- AndroidStudio2.2 使用NDK
- QQ大厅游戏 大家来找茬辅助