nginx的基本配置
2017-11-14 16:48
323 查看
一、基本简介
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
优点
Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版
Nginx 的源代码使用 2-clause BSD-like license。
Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。
二、nginx的安装
实验环境RedHat 6.52.1 解压,并修改配置文件
[root@server1 ~]# tar zxf nginx-1.10.3.tar.gz [root@server1 ~]# cd nginx-1.10.3 [root@server1 nginx-1.10.3]# vim src/core/nginx.h ##隐藏版本号,安全性 #define NGINX_VER "nginx" [root@server1 nginx-1.10.3]# vim auto/cc/gcc ##关闭gcc的debug调试模式,节省空间 # debug #CFLAGS="$CFLAGS -g"
2.2 编译安装,设置nginx的目录及各种模块比如openssl加密等
[root@server1 ~]# yum install -y gcc pcre-devel openssl-devel ##解决添加模块相应的依赖性 [root@server1 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-threads --with-file-aio --with-http_stub_status_module [root@server1 nginx-1.10.3]# make && make install
2.3 做相应的前提准备
[root@server1 ~]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin -u 800 nginx ##添加用户,并设置用户家目录 [root@server1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##做软连接方便调用 [root@server1 ~]# nginx ##启动nginx [root@server1 ~]# nginx -t ##检测nginx语法是否错误 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration fil 4000 e /usr/local/nginx/conf/nginx.conf test is successful [root@server1 ~]# nginx -s reload ##重新加载
三.nginx的多核绑定
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf user nginx nginx; ##nginx用户使用 worker_processes 2; ##二核cpu,开启2个进程 worker_cpu_affinity 01 10; ##01和10分别表示第一个CPU和第二个CPU(是用2进制进行计算的,比如三个001 .... events { worker_connections 65535; ##连接数,65535为最大的进程数 }
[root@server1 ~]# vim /etc/security/limits.conf ##最大连接数配置文件 nginx - nofile 65535
四、配置nginx证书加密
4.1配置文件
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf ##下面就直接以配置文件代替这一行
下面部分取消注释,将cert.key改为cert.pem
4.2 生成证书,pem为私钥,在此目录下查看Makefile可知pem包含证书与加密
生成证书的详细步骤[root@server1 ~]# cd /etc/pki/tls/certs [root@server1 certs]# make cert.pem ..... Country Name (2 letter code) [XX]:cn State or Province Name (full name) []:shaaxi Locality Name (eg, city) [Default City]:xi'an Organization Name (eg, company) [Default Company Ltd]:westos Organizational Unit Name (eg, section) []:linux Common Name (eg, your name or your server's hostname) []:server1 Email Address []:572025472@qq.com
下面server模块部分取消注释,将cert.key改为cert.pem,如下图的黑色部分
4.3 移至/usr/local/nginx/conf目录下,重新加载nginx,使之生效
注意如果配置了服务,就必须重新加载,检测语法可以快速定位错误,这俩步在下面以重新加载代替。[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/ [root@server1 certs]# nginx -t [root@server1 certs]# nginx -s reload
client 测试
五 定义多个不同域名站点的后端服务器
5.1 配置文件
[root@server1 conf]# vim /usr/local/nginx/conf/nginx.conf ##两个目录不同的域名 server { listen 80; server_name www.hello.org; location / { root /www; index index.html; } } server { listen 80; server_name bbs.hello.org; location / { root /bbs; index index.html; } }
5.2 创建默认发布目录,并编辑默认发布文件
[root@server1 conf]# mkdir /www /bbs [root@server1 conf]# cd /www/ [root@server1 www]# vim index.html [root@server1 www]# cat index.html <h1>www.hello.org</h1> [root@server1 www]# cd /bbs/ [root@server1 bbs]# vim index.html [root@server1 bbs]# cat index.html <h1>bbs.hello.org</h1
重新加载,并在client测试
每一个新出现的域名都需要在测试机做域名解析,下面这一步骤以”解析”代替
-bash-4.2# vim /etc/hosts 172.25.67.1 server1 www.hello.org bbs.hello.org
测试
六 负载均衡 round-robin(轮询),轮询是nginx中默认的负载均衡模式
配置文件http { upstream hello{ ##两个后端web服务器 server 172.25.67.2:80; server 172.25.67.3:80; } ..... } ..... server { listen 80; server_name www.hello.org; location / { #root /www; ##将之前实验的默认发布目录部分注释掉 #index index.html; proxy_pass http://hello } }
重新加载并测试
七 iphash和权重
配置文件upstream hello { ip_hash; ##ip_hash模式会根据来源IP和后端配置来做hash分配,确保固定IP只访问一个后端。 server 172.25.67.2:80; server 172.25.67.3:80; }
重新加载测试
配置文件
upstream hello { #ip_hash; server 172.25.67.2:80; server 172.25.67.3:80 weight=2; weight为权重,若不设置则默认为1 }
重新加载测试
八 主备
配置文件upstream hello { server 172.25.67.2:80; server 172.25.67.3:80; server 172.25.67.1:8080 backup; ##当前两主机崩溃时由172.25.67.1接手 }
重新加载,测试主备(server2与server3关闭httpd服务,server1开启httpd服务,顶替测试
九 域名重定向
注:配置完成后做好本地解析9.1 输入hello.org重定向到bbs.hello.org
配置文件server{ listen 80; server_name hello.org; rewrite ^(.*) //bbs.hello.org$1 permanent; }
重新加载并测试
9.2 添加luntan.hello.org重定向到bbs.hello.org
server{ listen 80; server_name hello.org luntan.hello.org; rewrite ^(.*) http://bbs.hello.org$1 permanent; }
和上一步测试一样
9.3 将bbs.hello.org重定向到https://bbs.hello.org
配置文件server { listen 443 ssl; server_name bbs; 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 /bbs; index index.html index.htm; } } .... server { listen 80; server_name bbs.hello.org; rewrite ^(.*) https://bbs.hello.org$1 permanent; location / { root /bbs; index index.html; } }
重新加载并测试
9.4 访问其他发布目录
新建发布目录文件[root@server1 conf]# mkdir /static/images -p [root@server1 conf]# cd /static/images/ [root@server1 images]# ls test.png
配置文件
server { listen 443 ssl; server_name bbs.hello.org; 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 /bbs; index index.html index.htm; } location /images { alias /static/images; ##location /images = bbs.hello.org/images alias后面是绝对路径 } }
访问bbs.hello.org/images/test.png –>> https://bbs.hello.org/images/test.png
重新加载服务并测试
相关文章推荐
- nginx基本配置说明
- Nginx服务器搭建和基本配置详解_nginx
- nginx基本配置与参数说明
- Nginx系列--02基本配置
- apache+nginx基本配置
- Http服务器基本配置[Windows + Nginx + MySQL + phpMyAdmin]
- lvs-nginx-haproxy基本配置与区别
- 手把手学习nginx基本配置
- nginx 基本配置分析
- nginx和php的基本配置说明
- Nginx服务器初期基本配置指南
- Nginx服务器搭建和基本配置详解
- nginx基本配置与参数说明
- Nginx的基本配置与优化
- Nginx - Windows下Nginx基本安装和配置
- Nginx 快速入门、基本配置
- The server of Nginx(二)——Nginx基本功能配置
- 绝对详细!Nginx基本配置、性能优化指南
- Nginx配置性能基本优化的方法
- Nginx功能简介与基本配置总结