centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡
# 用了nginx for win很久,安装也是超级简单。
# 还是用一下linux版的吧。环境是centos 6.5 或7.5 x64
# 安装开始:
# 先安装依赖
yum install gcc-c++
yum -y install pcre*
yum -y install openssl*
###### yum 方式:
yum install epel-release # 使用 epel 库,这个库带有很多第三方软件 如 nginx
yum update # 全部更新
yum install nginx # 安装 nginx
###### tar 包方式:
# 下载,可以wget 目前最新1.15.3
cd /opt
wget http://nginx.org/download/nginx-1.12.2.tar.gz tar zxf nginx-1.12..tar.gz
cd nginx-1.12. # 指定安装目录 、编译安装
./configure --prefix=/opt/nginx
make && make install # 检查测试
/opt/nginx/sbin/nginx -t
# 启动 停止 退出
/opt/nginx/sbin/nginx -h # 帮助
/opt/nginx/sbin/nginx # 启动
/opt/nginx/sbin/nginx -s stop
/opt/nginx/sbin/nginx -s quit
# 如果是centos7以上,已经注册为系统服务的:
systemctl stop nginx
systemctl start nginx
#---------------- 官方文档: -s 参数------------
# nginx -h # 可在帮助中看到以下信息
# stop — fast shutdown
# quit — graceful shutdown
# reload — reloading the configuration file
# reopen — reopening the log files
#----------------------------------------------
如果需要开机启动,可安装为系统服务,以centos7为例:
# 安装完成时,自动添加了 /usr/lib/systemd/system/nginx.service 文件 [Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=
KillMode=process
PrivateTmp=true [Install]
WantedBy=multi-user.target # 接下来,只需要简单几步即可:
systemctl enable nginx # 设为开机启动
systemctl status nginx # 查看状态,可以看到服务脚本路径
systemctl start nginx
systemctl stop nginx
# 查看进程,端口 检查运行情况
ps aux |grep nginx # master worker 至少各一个
netstat -tulnp | grep : # 如果想要命令方便执行,可将路径加入PATH变量中
vim /etc/profile # 加入 行
export NGINX_HOME=/opt/nginx
export PATH=$NGINX_HOME/sbin:$PATH source /etc/profile # 生效 # 或者使用添加软连接的方式:
ln -s /opt/nginx/sbin/nginx /usr/bin/nginx
#-------------------- 配置文件 nginx.conf ---------------------------
以下版本有一些简单的优化, 注意那些写有注释的行。
user nginx;
worker_processes auto; # 进程数,一般设置为CPU核数,或者 auto
pid /var/run/nginx.pid; # 记录进程PID文件,可以不启用 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
use epoll; # 使用高效的 epoll 方式
worker_connections 65535; # 单个worker进程同时打开的最大连接数 跟系统的 ulimit -n 有关
} http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off; # 隐藏web出错时的版本号
CentOS 本身也要修改一下最大进程数和最大打开文件数 vim /etc/security/limits.conf 永久修改,重启后生效。
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
* soft nproc 16384
* hard nproc 16384
使用 ulimit -a 查看结果 。 另外,ulimit -n 65535 可以改 open files , ulimit -u 可以改 max user process 。 但是都不是永久更改。
可能还需要更改下 90-nproc.conf ( CentOS 7是 20-nproc.conf )中的 数字 vim /etc/security/limits.d/90-nproc.conf
有人改 /etc/systemd/system.conf 的两行:DefaultLimitNOFILE=65534 和 DefaultLimitNPROC=65534 。亲测不起作用。重启后 ulimit -a 得到的还是 /etc/security/limits.conf 中设置的值。
虚拟主机
超简单配置 基于不同域名的虚拟主机,其实就是:根据访问的不同网址对应不同的nginx中的不同文件夹。
先备份一个conf/nginx.conf文件,然后修改 http{}中的server, 删除原有的,示例如下:
## 注意:下面仅仅是http{}部分,不是nginx.conf的全部
http {
server_tokens on;
include mime.types;
default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { # 创建服务
listen 80;
server_name wwww.node-6.com; # 域名
## location 匹配URL路径地址,/ 表示匹配所有路径 =开头表示精确匹配 ~区分大小写 ~*不区分大小写(默认不区分)
location / {
root data/www; # 根目录对应真实文件夹
index index.html index.htm index.jsp;
}
} server {
listen 80;
server_name bbs.node-6.com;
location / {
root data/bbs;
index index.html index.htm index.jsp;
}
} }
对应地,需要在nginx下建立目录 data/www 和 data/bbs 以及各自目录下的文件。
执行 nginx -s reload 重新载入配置文件。 配置好DNS解析或hosts文件,浏览器测试访问。
超简单配置 基于不同端口号的虚拟主机,其实就是:根据访问网址的不同端口 对应不同的nginx中的不同文件夹。
和上面的配置差不多,只不过域名相同,listen不同:
server { # 服务
listen ;
server_name wwww.node-6.com; # 域名
location / {
root data/www; # 根目录对应真实文件夹
index index.html index.htm index.jsp;
}
}
server {
listen ;
server_name wwww.node-6.com;
location / {
root data/bbs;
index index.html index.htm index.jsp;
}
}
server {
listen ;
server_name wwww.node-6.com;
location / {
root data/img;
index index.html index.htm index.jsp;
}
}
同样,nginx -s reload 即可生效。
配置 nginx 反向代理
和上面的虚拟主机配置差不多,既可以不同端口,也可以不同域名,关键词 proxy_pass 示例如下:
## 不同端口的反向代理 server{}部分 server { # 服务
listen ;
server_name wwww.node-.com; # 域名
location / {
proxy_pass http://127.0.0.1:8080; # 反向代理本地 tomcat
index index.html index.htm index.jsp;
}
}
server {
listen ;
server_name www.node-.com; # 域名
location / {
proxy_pass http://127.0.0.1:8081; # 反向代理
index index.html index.htm index.jsp;
}
}
## 不同域名的反向代理 server{}部分 server { # 服务
listen ;
server_name wwww.node-.com; # 域名
location / {
proxy_pass http://127.0.0.1:8080; # 反向代理本地 tomcat
index index.html index.htm index.jsp;
}
}
server {
listen ;
server_name bbs.node-.com; # 域名
location / {
proxy_pass http://127.0.0.1:8081; # 本地另一个 tomcat
index index.html index.htm index.jsp;
}
}
配置完成后,同样,nginx -s reload 即可生效。
不同Location 做反向代理,示例如下:
## 不同Location的反向代理 server{}部分
## 注意目标地址末尾要有/ server { # 服务
listen ;
server_name www.node-.com; # 域名
location /www { # 末尾有无/不影响
proxy_pass http://127.0.0.1:8080/; # 末尾一定要/
index index.html index.htm index.jsp;
}
location /bbs {
proxy_pass http://127.0.0.1:8081/;
index index.html index.htm index.jsp;
}
}
负载均衡:
使用 upstream 方式,配置也很简单:在server{} 上面定义一个 upstream backServer 然后在proxy_pass中指向backServer 示例如下:
## 以下部分全部应在 http{}内:
## 定义多个上游服务器(真实业务)服务器的IP和端口,默认采用轮询机制
upstream backServer{
server 127.0.0.1:;
server 192.168.112.5:;
} server {
listen ;
server_name www.node-.com; # 域名
location / {
proxy_pass http://backServer/; # 末尾一定要/
index index.html index.htm index.jsp;
}
}
负载均衡的方式:
轮询机制:轮流访问,非常均匀
权重机制:使用weight配置比例
ip hash: nginx获取IP地址hash运算固定分配到某服务器上,可以解决session共享问题
fair :第三方
url绑定:第三方
权重机制的设置和上面的简单设置差不多,只在目标后面加了weight。示例如下:
upstream backServer{
server 127.0.0.1: weight=;
server 192.168.112.5: weight=;
}
IP绑定方式,只是多了行 ip_hash;
upstream backServer{
server 127.0.0.1:;
server 192.168.112.5:;
ip_hash;
}
一般情况下,可能实际生产环境,配置轮询机制或者权重机制比较多见。
如果上游服务器有个突然挂了怎么办?
所以,要设置好nginx的故障转移,示例如下:
## http{}中的两段配置
upstream backServer{
server 127.0.0.1:;
server 127.0.0.1:;
server 192.168.112.5:;
} server {
listen ;
server_name www.node-.com; # 域名
location / {
proxy_pass http://backServer/; # 末尾一定要/
## 故障转移:设置超时时间
proxy_connect_timeout 1s;
proxy_send_timeout 1s;
proxy_read_timeout 1s;
index index.html index.htm index.jsp;
}
}
结合 Keepalived 实现主备模式
虽然有了负载均衡,但nginx本身挂了怎么办?所以需要主备模式。
一旦主nginx挂了,备nginx自动接替。而主nginx恢复后又会自动承担主机。
简单的方法是结合keepalived来实现:
# keepalived 实现主备模式 ----------------------------------------------
yum install keepalived # MASTER: vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.52.5
smtp_connect_timeout 30
router_id LVS_DEVEL
} vrrp_script chk_http_port {
script "/opt/scripts/nginx_check.sh" # file path
interval 2 # per 2s
weight 2
} vrrp_instance VI_1 {
state MASTER # in backup server would be "BACKUP"
interface ens33 # Ethernet card
virtual_router_id 51 # MASTER & BACKUP must be same.
priority 100 # MASTER big than BACKUP
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port # execute check
}
virtual_ipaddress {
192.168.52.16 # VRRP H virtual addr
}
}
# BACKUP: vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.52.5
smtp_connect_timeout 30
router_id lvsback
} vrrp_script chk_http_port {
script "/opt/scripts/nginx_check.sh" # file path
interval 2 # per 2s check
weight 2
} vrrp_instance VI_1 {
state BACKUP # in backup server
interface ens33 # Ethernet card
virtual_router_id 51 # MASTER & BACKUP must be same.
priority 90 # MASTER big than BACKUP
advert_int 1 # heartbeat 1s
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port # execute check
}
virtual_ipaddress {
192.168.52.16 # virtual addr
}
}
# 注意keepalived.conf文件主备稍有不同,详见带有注释的行。
# router_id 测试中并不重要,没有影响。
# vrrp_script chk_http_port 定义了检测脚本及间隔秒数
# virtual_router_id 51 主备必须一致
# priority 90 优先级备比主数字稍低一些
# track_script 调用脚本并执行
# virtual_ipaddress 用来对外公布的地址. 主备相同为了实现故障转移. # MASTER & BACKUP: vim /opt/scripts/nginx_check.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq ];
then
/opt/nginx/sbin/nginx || killall keepalived
fi
# 此脚本的作用是检查有无 nginx 进程,如果没有则试图启动 nginx,
# 若nginx尝试启动失败,则杀死keepalived进程,从而实现故障自动转移。
# 测试时访问 192.168.52.16 可以看到主内容,故意给主nginx制造错误后,杀死 nginx 进程,
# 而在尝试启动 nginx 失败后,keepalived 进程将被结束,备机自动接管并将内容呈现。
# 测试时若 nginx 没有错误,即使手动结束 nginx 进程,keepalived 也会借助以上脚本,自动启动 nginx
URL重写:
参考:http://www.cnblogs.com/czlun/articles/7010604.html
https://www.linuxidc.com/Linux/2014-01/95493.htm
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
。关键字:其中关键字error_log不能改变
。正则:perl兼容正则表达式语句进行规则匹配
。替代内容:将正则匹配的内容替换成replacement
。flag标记:rewrite支持的flag标记
flag标记说明:
last #本条规则匹配完成后,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
rewrite参数的标签段位置:
server, location, if
SSL
现如今,很多网站都使用了https 。 nginx对https的设置很简单,在拥有证书的前提下。将证书文件放入 nginx/cert目录下:
然后配置文件中添加 SSL设置即可,以下示例: 也可参考 :https://www.cnblogs.com/tianhei/p/7726505.html
server {
listen 443;
server_name aaa.bbb.com;
ssl on;
root /var/www/html/aaa/Public;
ssl_certificate cert/2254667_aaa.bbb.com.pem;
ssl_certificate_key cert/2254667_aaa.bbb.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
index index.html index.htm index.php;
} location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
最后验证 https://aaa.bbb.com 若出现 小锁标志,点击小锁后显示证书有效,即设置成功。
实际配置文件示例: nginx/default.conf
centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡的更多相关文章
- linux下nginx【反向代理】配置【负载均衡】配置
nginx 可以配置多个端口: 1.10088端口 配置反向代理,消除跨域问题. 2.10087端口 配置ip_hash模式的负载均衡,ip_hash可以绕开解决session共享的问题. nginx ...
- 七、CentOS 6.5 下 Nginx的反向代理和负载均衡的实现
CentOS 6.5 下 Nginx的反向代理和负载均衡的实现 * 修复上面文章的问题: 复制出一个tomcat2之后,修改service.xml文件时,要修改三个端口: 1. <!-- 800 ...
- [原]生产环境下的nginx.conf配置文件(多虚拟主机)
[原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...
- nginx学习笔记(8)虚拟主机名---转载
通配符名字正则表达式名字其他类型的名字优化兼容性 虚拟主机名使用server_name指令定义,用于决定由某台虚拟主机来处理请求.具体请参考<nginx如何处理一个请求>.虚拟主机名可以使 ...
- nginx反向代理+缓存开启+url重写+负载均衡(带健康探测)的部署记录
在日常运维工作中,运维人员会时常使用到nginx的反向代理,负载均衡以及缓存等功能来优化web服务性能. 废话不多说,下面对测试环境下的nginx反向代理+缓存开启+url重写+负载均衡(带健康探测) ...
- Nginx+keepalived做双机热备加tomcat负载均衡
Nginx+keepalived做双机热备加tomcat负载均衡 环境说明: nginx1:192.168.2.47 nginx2:192.168.2.48 tomcat1:192.168.2.49 ...
- Nginx 部署、反向代理配置、负载均衡
Nginx 部署.反向代理配置.负载均衡 最近我们的angular项目部署,我们采用的的是Nginx,下面对Nginx做一个简单的介绍. 为什么选择Nginx 轻:相比于Apache,同样的web服务 ...
- 即时通讯新手入门:一文读懂什么是Nginx?它能否实现IM的负载均衡?
本文引用了“蔷薇Nina”的“Nginx 相关介绍(Nginx是什么?能干嘛?)”一文部分内容,感谢作者的无私分享. 1.引言 Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡 ...
- 通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布
前提 前段时间顺利地把整个服务集群和中间件全部从UCloud迁移到阿里云,笔者担任了架构和半个运维的角色.这里详细记录一下通过Nginx.Consul.Upsync实现动态负载均衡和服务平滑发布的核心 ...
随机推荐
- Docker镜像加速器配置
一.为什么要配置Docker镜像加速器 因为我们默认pull的docker镜像是从Docker Hub来下载,由于其服务器在国外,速度会比较慢.因此我们可以配置成国内的镜像仓库,这样可以加速镜像的上传 ...
- 蓝鲸DevOps深度解析系列(2):蓝盾流水线初体验
关注嘉为科技,获取运维新知 前面一篇文章<蓝鲸DevOps深度解析系列(1):蓝盾平台总览>,我们总览了蓝鲸DevOps平台的背景.应用场景.特点和能力: 接下来我们继续解析蓝盾平台的 ...
- expect简单自动交互-用于密码、命令输入
1. 安装expect #yum -y install expect 2. 新建.exp文件,用于ssh交换机 #vi exp.exp #!/bin/expect set f [open ipfile ...
- 《c++ concurrency in action》读书笔记1
1. 什么是并发通俗来说,并发指两个或者多个独立的事件(活动)同时发生.比如,一边走路一边说话,两个手同时做不同的事情.计算机系统的并发是指一个系统并行处理多个独立的事件(活动), 而不是按顺序或者一 ...
- Linux之redis-cluster(集群配置)
redis-cluster配置 为什么要用redis-cluster 1.并发问题 redis官方声称可以达到 10万/每秒,每秒执行10万条命令假如业务需要每秒100万的命令执行呢? 2.数据量 ...
- Python操作MongoDB和Redis
1. python对mongo的常见CURD的操作 1.1 mongo简介 mongodb是一个nosql数据库,无结构化.和去中心化. 那为什么要用mongo来存呢? 1. 首先.数据关系复杂,没有 ...
- .net邮件发送帮助类
代码如下: using System; using System.Collections.Generic; using System.Text; using System.Configuration; ...
- js 获取昨天,今天,本周,上周,季度等时间范围(封装的js)
(function ($, ht) { "use strict"; $.extend(ht, { clickTimeRange:function(){ //点击重置按钮,时间文本框 ...
- 手机上 input submit ios和andirod样式不统一
-webkit-appearance:none;
- python 基础语法练习回顾
#!/usr/bin/python# -*- coding: UTF-8 -*-import timeimport calendar student = {"age": 7,&qu ...