码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 服务搭建

时间:2016-09-17 16:31:17      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

Nginx服务                                                                                          
nginx在系统上占用更少的系统资源,在特定的场景应用,能支持更多的高并发                                                            
是一个高性能的http server和代理服务器软件。                                                                      
优点:                                                                                              
    高并发,能支持1-2万甚至更多的并发链接(静态小文件)                                                                  
    内存消耗少:在3万并发链接下,开启10个nginx进程消耗不到200M                                                          
    可以做http反向代理,即负载均衡,相当于专业的haproxy软件或lvs的功能                                                     
    内置对RS服务器健康检查功能:如过nginx Proxy后端某台web服务器宕机,不会影响前端访问,这个功能比较弱,需要后续改进。                                                                     
    通过cache插件(cache_purge)可以实现类squid等专业的缓存软件实现功能。                                                
    Nginx最主要的有点:支持kqueue(freeBSD4.1+),epoll(linux2.6+)等网络io事件模型,                                 
    由此来支持高并发。                                                                                    
                                                                                                 
apache使用的是select模型低效,Nginx使用的是epoll模型。                                                           
                                                                                                 
==================================================================================================
                                                                                                 
nginx安装:                                                                                         
    rpm方式安装                                                                                      
   [root@Serv ~]# yum install zlib zlib-devel pcre -y                                                           
  [root@Serv ~]# rpm -ivh nginx-0.8.34-1.x86_64.rpm                                                            
                                                                                                 
配置文件                                                                                             
/etc/nginx/nginx.conf(rpm安装)                                                                     
主目录                                                                                              
/usr/shar/nginx/html/                                                                            
查看版本                                                                                             
[root@centos ~]# /usr/sbin/nginx -v                                                              
nginx version: nginx/1.0.15                                                                      
对配置语法检查                                                                                          
[root@centos ~]# /usr/sbin/nginx -t                                                              
启动                                                                                               
[root@centos ~]# /etc/init.d/nginx start                                                         
停止                                                                                               
[root@centos ~]# /etc/init.d/nginx stop                                                          
改了配置,平滑启动                                                                                        
[root@centos ~]# /etc/init.d/nginx reload                                                        
                                                                                                 
/usr/sbin/nginx -c /etc/nginx/nginx.conf                                                         
nginx的工作模式是1个master进程+N个worker进程                                                                 
                                                                                                 
                                                                                                 
=============================================================                                    
编译安装nginx                                                                                        
pcre库需要,中文兼容prel兼容正则表达式                                                                          
官网下载pcre                                                                                         
tar zxf pcre.tat.gz                                                                              
cd pcre-8.31/                                                                                    
./configure                                                                                      
make && make install                                                                             
cd ../                                                                                           
                                                                                                 
下载官方nginx源码包                                                                                     
#tar xf nginx.tar.gz                                                                              

#./configure --user=nginx --group=nginx --prefix=/ser_app/nginx-1.10  --with-http_stub_status_module --with-http_ssl_module                                            
#make && make install                                                                             
                                                                                                 
语法检查:                                                                                            
[root@Serv ~]#/ser_app/nginx/sbin/nginx -t                                                                     
启动                                                                                               
[root@Serv ~]#/ser_app/nginx/sbin/nginx                                                                        
端口检查                                                                                             
[root@centos nginx-1.10.1]# netstat -lnt                                                         
检查进程                                                                                             
[root@centos nginx-1.10.1]# ps -ef|grep ngnix                                                    
查看端口是不是nginx                                                                                     
# lsof -i :80                                                                                      
                                                                                                 
打开浏览器输入服务器的ip地址,看到welcome to nginx,表示安装正常                                                        
如果看不到这些,查看iptables防火墙和selin是否关闭,                                                                 
[root@centos data]# /etc/init.d/iptables stop                                                    
-->这是关闭防火墙的命令,如果是有外网ip生产环境请允许80端口访问,而不是关闭防火墙,                                                    
允许命令如:iptables -I INPUT -p tcp --dport 80 -j ACCEPT                                              
非正式环境可以禁止防火墙开机自起,便于学习nginx服务。                                                                    
chkconfig iptables off                                                                           
查看当前防火墙状态                                                                                        
[root@Serv ~]# /etc/init.d/iptables status                                                                      
                                                                                                 
===========================================================                                      
部署一个简单的ngnix web站点                                                                               
nginx的默认站点目录,是nginx安装目录下的/ser_app/nginx/html目录,                                                  
这是可以从nginx主配置文件/ser_app/nginx/conf/nginx.conf中查到。                                                
将网站网页放到该目录下即可/ser_app/nginx/html/                                                                
打开浏览器输入服务器ip地址,即可                                                                                
=========================================================                                        
nginx配置说明:                                                                                       
nginx的目录结构说明                                                                                     
[root@centos ser_app]# tree  nginx                                                               
nginx                                                                                            
├── client_body_temp                                                                             
├── conf        //nginx的所有配置文件目录                                                                 
│   ├── fastcgi.conf    //fastcgi的配置                                                             
│   ├── fastcgi.conf.default                                                                     
│   ├── fastcgi_params      //fastcgi的参数文件                                                       
│   ├── fastcgi_params.default                                                                   
│   ├── koi-utf                                                                                  
│   ├── koi-win                                                                                  
│   ├── mime.types                                                                               
│   ├── mime.types.default                                                                       
│   ├── nginx.conf      //nginx默认的主配置文件                                                          
│   ├── nginx.conf.default                                                                       
│   ├── scgi_params                                                                              
│   ├── scgi_params.default                                                                      
│   ├── uwsgi_params                                                                             
│   ├── uwsgi_params.default                                                                     
│   └── win-utf                                                                                  
├── fastcgi_temp    //临时目录                                                                       
├── html    //这是编译时nginx的默认站点目录                                                                  
│   ├── 50x.html    //错误页面优雅显示文件,例如:出现502会调用此页面                                                  
│   ├── index.html  //默认首页                                                                       
│   └── index.html.bak                                                                           
├── logs    //nginx 默认的日志目录,包括错误日志及访问日志                                                          
│   ├── access.log  //访问日志文件                                                                     
│   ├── error.log   //错误日志文件                                                                     
│   └── nginx.pid   //nginx的进程pid文件                                                              
├── proxy_temp                                                                                   
├── sbin    //这是nginx命令目录                                                                        
│   └── nginx                                                                                    
├── scgi_temp   //临时目录                                                                           
└── uwsgi_temp  //临时目录                                                                           
                                                                                                 
-----------------------------------------------------------                                      
配置nginx.conf:                                                                                    
配置基于域名的虚拟主机,即创建相关站点目录及文件                                                                         
共包含blog.xiang8888.com,www.xiang8888.com,bbs.xiang8888.com三个虚拟主机目录:                               
[root@centos conf]# mkdir /data/www/{www,bbs,blog} -p                                            
[root@centos conf]# for n in www blog bbs;do echo "$n" >/data/www/$n/index.html;done             
                                                                                                 
授权                                                                                               
[root@centos conf]# chown -R nginx:nginx /data/www                                               
创建日志目录                                                                                           
[root@centos conf]# mkdir /app/log -p                                                            
修改配置文件,注意语句文件后面都要有分号                                                                             
[root@centos conf]# vim nginx.conf                                                               
[root@centos conf]# echo www >/data/www/www/index.html                                           
检查语法                                                                                             
[root@centos conf]# ../sbin/nginx -t                                                             
平滑重启,apache使用graceful                                                                            
[root@centos conf]# ../sbin/nginx -s reload                                                      
检查nginx重启状况:                                                                                     
[root@centos conf]# ps -ef|grep nginx                                                            
root      23036      1  0 Aug15 ?        00:00:00 nginx: master process /ser_app/nginx/sbin/nginx
nginx     23836  23036  0 00:14 ?        00:00:00 nginx: worker process                          
nginx     23837  23036  0 00:14 ?        00:00:00 nginx: worker process                          
nginx     23838  23036  0 00:14 ?        00:00:00 nginx: worker process                          
                                                                                                 
dns域名解析:                                                                                         
C:\Windows\System32\drivers\etc\hosts                                                            
添加域名和配置的服务器器的对应解析                                                                                
192.168.1.106 www.xiang8888.com bbs.xiang8888.com blog.xiang8888.com                             
                                                                                                 
开启多个虚拟主机,只需要在nginx.conf中多加几个server标签就可以了,                                                        
检查语法:                                                                                            
[root@centos conf]# ../sbin/nginx -t                                                             
平滑重启                                                                                             
[root@centos conf]# ../sbin/nginx -s reload                                                      
                                                                                                 
向其他标签添加内容                                                                                        
[root@centos conf]# echo "bbs.xiang8888.com">/data/www/bbs/index.html                            
[root@centos conf]# echo blog.xiang8888.com > /data/www/blog/index.html                          
添加完后一定要hosts或者dns域名解析                                                                            
                                                                                                 
分别向虚拟主机里面添加日志文件                                                                                  
访问日志,错误日志,及pid日志日志                                                                               
                                                                                                 
nginx.conf配置优化                                                                                   
模仿apache,在外面新建一个extra目录,然后include虚拟主机配置文件                                                        
[root@centos conf]# mkdir extra/                                                                 
[root@centos conf]# cp nginx.conf extra/nginx_vhosts.conf                                        
将虚拟主机server全部放在extra/nginx_vhosts.conf中,nginx中删除server配置                                         
使用include extra/nginx_vhosts.conf包含                                                              
检查和重启                                                                                            
[root@centos conf]# ../sbin/nginx -t                                                             
[root@centos conf]# ../sbin/nginx -s reload                                                      
                                                                                                 
------------------------------------------------------------------------                         
基于域名的虚拟主机,优化nginx配置文件                                                                            
可以将nginx_vhosts.conf的各个虚拟主机分离开,在主配置文件中包含                                                         
方法同上,这样方便管理,也不会受其他虚拟主机的干扰.                                                                       
                                                                                                 
配置nginx状态信息虚拟主机                                                                                  
cat >> /ser_app/nginx/conf/extra/nginx_vhosts.conf <<EOF                                         
server {                                                                                         
    listen       80;                                                                             
    server_name  status.xiangsheng99.org;                                                        
    location / {                                                                                 
        stub_status on;                                                                          
        access_log  off;                                                                         
    }                                                                                            
}                                                                                                
在浏览器输入http://status.xiangsheng99.org即可看到状态信息                                                     
------------------------------------------------------------------------                         
基于端口的虚拟主机配置                                                                                      
基于端口的虚拟主机在生产环境中的应用不多见,一般为公司内部员工提供访问,                                                             
如页面后台,cms发布等                                                                                     
                                                                                                 
配置监听的端口:                                                                                         
编辑nginx_vhosts.conf,将listen监听的端口修改即可。                                                            
端口检查,查看使用了哪些端口                                                                                   
[root@centos extra]# netstat -lnt                                                                
只要端口没被其他服务占用即可使用                                                                                 
检查语法,重启,检查端口                                                                                     
[root@centos extra]# ../../sbin/nginx -t                                                         
[root@centos extra]# ../../sbin/nginx -s reload                                                  
[root@centos extra]# netstat -lnt|grep 80                                                        
                                                                                                 
在浏览器中输入                                                                                          
http://www.xiangsheng99.org:8060                                                                 
http://www.xiangsheng99.org:8070                                                                 
http://www.xiangsheng99.org:8080                                                                 
能够访问,说明成功                                                                                        
------------------------------------------------------------------------                         
基于ip的虚拟主机配置                                                                                      
增加ip使用up,删除ip使用down                                                                              
ifconfig eth0:190 192.168.1.190 netmask 255.255.255.0 up                                         
ifconfig eth0:191 192.168.1.191 netmask 255.255.255.0 up                                         
ifconfig eth0:192 192.168.1.192 netmask 255.255.255.0 up                                         
                                                                                                 
在各自的vhosts.conf中添加一行                                                                             
在端口上也要加上ip,如                                                                                     
listen      192.168.1.190:8060                                                                   
server_name 192.168.1.190;                                                                       
                                                                                                 
语法检查,重启,端口检查                                                                                     
[root@centos extra]# ../../sbin/nginx -t                                                         
[root@centos extra]# ../../sbin/nginx -s reload                                                  
[root@centos extra]# netstat -lnt|grep 80                                                        
                                                                                                 
在浏览器中输入http://192.168.1.190:8060即可                                                               
                                                                                                 
=========================================================================                        
给nginx虚拟主机配置别名:                                                                                  
只需要在extra/nginx_vhosts.conf文件中的server_name 行加入网站别名就好了,                                           
注意dns域名解析。                                                                                       
                                                                                                 
语法检查,重启,端口检查                                                                                     
[root@centos extra]# ../../sbin/nginx -t                                                         
[root@centos extra]# ../../sbin/nginx -s reload                                                  
[root@centos extra]# netstat -lnt|grep 80                                                        
浏览器中输入别名弹出网页,就代表配置成功                                                                             
                                                                                                 
========================================================================                         
配置多个nginx实例                                                                                      
实例参数                                                                                             
nginx -h    //查看nginx参数用法                                                                        
Options:                                                                                         
-?,-h       : this help                                                                          
-v          : show version and exit 检查版本                                                         
-V          : show version and configure options then exit.检查编译参数                                
-t          : test configuration and exit                                                        
-T          : test configuration, dump it and exit                                               
-q          : suppress non-error messages during configuration testing                           
-s signal   : send signal to a master process: stop, quit, reopen, reload                        
-p prefix     : set prefix path (default: /ser_app/nginx-1.10/)                                  
-c filename   : set configuration file (default: conf/nginx.conf)                                
-g directives : set global directives out of configuration file                                  
                                                                                                 
将原来的nginx,的配置文件conf复制一份,不能重名,修改参数,                                                               
如:域名,日志文件,不能喝之前的冲突                                                                               
记得要dns域名解析                                                                                       
检查语法,启动nginx                                                                                     
../sbin/nginx -c /ser_app/nginx/cmsconf/nginx.conf -t                                            
../sbin/nginx -c /ser_app/nginx/cmsconf/nginx.conf                                               
如果有端口被占用,有两种方法:                                                                                  
    添加ip地址 或者修改端口号                                                                               
打开浏览器访问网址即可,弹出页面内容代表配置成功                                                                         
                                                                                                 
                                                                                                 

Nginx 服务搭建

标签:

原文地址:http://www.cnblogs.com/erick2/p/5878838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!