Nginx 服务搭建

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方式安装                                                                                      
   [[email protected] ~]# yum install zlib zlib-devel pcre -y                                                           
  [[email protected] ~]# rpm -ivh nginx-0.8.34-1.x86_64.rpm                                                            
                                                                                                 
配置文件                                                                                             
/etc/nginx/nginx.conf(rpm安装)                                                                     
主目录                                                                                              
/usr/shar/nginx/html/                                                                            
查看版本                                                                                             
[[email protected] ~]# /usr/sbin/nginx -v                                                              
nginx version: nginx/1.0.15                                                                      
对配置语法检查                                                                                          
[[email protected] ~]# /usr/sbin/nginx -t                                                              
启动                                                                                               
[[email protected] ~]# /etc/init.d/nginx start                                                         
停止                                                                                               
[[email protected] ~]# /etc/init.d/nginx stop                                                          
改了配置,平滑启动                                                                                        
[[email protected] ~]# /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                                                                             
                                                                                                 
语法检查:                                                                                            
[[email protected] ~]#/ser_app/nginx/sbin/nginx -t                                                                     
启动                                                                                               
[[email protected] ~]#/ser_app/nginx/sbin/nginx                                                                        
端口检查                                                                                             
[[email protected] nginx-1.10.1]# netstat -lnt                                                         
检查进程                                                                                             
[[email protected] nginx-1.10.1]# ps -ef|grep ngnix                                                    
查看端口是不是nginx                                                                                     
# lsof -i :80                                                                                      
                                                                                                 
打开浏览器输入服务器的ip地址,看到welcome to nginx,表示安装正常                                                        
如果看不到这些,查看iptables防火墙和selin是否关闭,                                                                 
[[email protected] data]# /etc/init.d/iptables stop                                                    
-->这是关闭防火墙的命令,如果是有外网ip生产环境请允许80端口访问,而不是关闭防火墙,                                                    
允许命令如:iptables -I INPUT -p tcp --dport 80 -j ACCEPT                                              
非正式环境可以禁止防火墙开机自起,便于学习nginx服务。                                                                    
chkconfig iptables off                                                                           
查看当前防火墙状态                                                                                        
[[email protected] ~]# /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的目录结构说明                                                                                     
[[email protected] 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三个虚拟主机目录:                               
[[email protected] conf]# mkdir /data/www/{www,bbs,blog} -p                                            
[[email protected] conf]# for n in www blog bbs;do echo "$n" >/data/www/$n/index.html;done             
                                                                                                 
授权                                                                                               
[[email protected] conf]# chown -R nginx:nginx /data/www                                               
创建日志目录                                                                                           
[[email protected] conf]# mkdir /app/log -p                                                            
修改配置文件,注意语句文件后面都要有分号                                                                             
[[email protected] conf]# vim nginx.conf                                                               
[[email protected] conf]# echo www >/data/www/www/index.html                                           
检查语法                                                                                             
[[email protected] conf]# ../sbin/nginx -t                                                             
平滑重启,apache使用graceful                                                                            
[[email protected] conf]# ../sbin/nginx -s reload                                                      
检查nginx重启状况:                                                                                     
[[email protected] 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标签就可以了,                                                        
检查语法:                                                                                            
[[email protected] conf]# ../sbin/nginx -t                                                             
平滑重启                                                                                             
[[email protected] conf]# ../sbin/nginx -s reload                                                      
                                                                                                 
向其他标签添加内容                                                                                        
[[email protected] conf]# echo "bbs.xiang8888.com">/data/www/bbs/index.html                            
[[email protected] conf]# echo blog.xiang8888.com > /data/www/blog/index.html                          
添加完后一定要hosts或者dns域名解析                                                                            
                                                                                                 
分别向虚拟主机里面添加日志文件                                                                                  
访问日志,错误日志,及pid日志日志                                                                               
                                                                                                 
nginx.conf配置优化                                                                                   
模仿apache,在外面新建一个extra目录,然后include虚拟主机配置文件                                                        
[[email protected] conf]# mkdir extra/                                                                 
[[email protected] conf]# cp nginx.conf extra/nginx_vhosts.conf                                        
将虚拟主机server全部放在extra/nginx_vhosts.conf中,nginx中删除server配置                                         
使用include extra/nginx_vhosts.conf包含                                                              
检查和重启                                                                                            
[[email protected] conf]# ../sbin/nginx -t                                                             
[[email protected] 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监听的端口修改即可。                                                            
端口检查,查看使用了哪些端口                                                                                   
[[email protected] extra]# netstat -lnt                                                                
只要端口没被其他服务占用即可使用                                                                                 
检查语法,重启,检查端口                                                                                     
[[email protected] extra]# ../../sbin/nginx -t                                                         
[[email protected] extra]# ../../sbin/nginx -s reload                                                  
[[email protected] 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;                                                                       
                                                                                                 
语法检查,重启,端口检查                                                                                     
[[email protected] extra]# ../../sbin/nginx -t                                                         
[[email protected] extra]# ../../sbin/nginx -s reload                                                  
[[email protected] extra]# netstat -lnt|grep 80                                                        
                                                                                                 
在浏览器中输入http://192.168.1.190:8060即可                                                               
                                                                                                 
=========================================================================                        
给nginx虚拟主机配置别名:                                                                                  
只需要在extra/nginx_vhosts.conf文件中的server_name 行加入网站别名就好了,                                           
注意dns域名解析。                                                                                       
                                                                                                 
语法检查,重启,端口检查                                                                                     
[[email protected] extra]# ../../sbin/nginx -t                                                         
[[email protected] extra]# ../../sbin/nginx -s reload                                                  
[[email protected] 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地址 或者修改端口号                                                                               
打开浏览器访问网址即可,弹出页面内容代表配置成功

时间: 2024-10-13 22:33:19

Nginx 服务搭建的相关文章

Nginx服务搭建及配置简介

在各种网站服务器当中除了Apache外,还有一款轻量级的网站服务器软件Nginx,其特点是稳定性较好以及较低系统资源消耗,以及对HTTP并发连接的处理能力. 实验环境 Redhat6.5版本 Nginx1.6版本 服务搭建 1.编译安装Nginx yum install pcre-devel zlib-devel gcc gcc-c++ -y //安装支持软件包 tar xzvf nginx-1.6.0.tar.gz -C /opt //解压Nginx软件包 cd /opt/nginx-1.6.

Nginx服务搭建

###配置环境 Linux A 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux nginx版本:nginx-1.6.2.tar.gz 1.nginx安装之前关闭selinux 关闭防火墙iptables: 2.nginx 官方网站 http://nginx.org/  (注意找stable稳定版本) 3.跟新yum源:此处是阿里的yunm源 wget -O /etc/

windows nginx 服务搭建

1,去官网下载对应的安装包 地址:http://nginx.org 下载到目录下解压,进入nginx文件目录(解压目录注意不要有中文路径,进入文件后,不要双击nginx.exe) 下载完成后,解压缩,运行cmd,使用命令进行操作,不要直接双击nginx.exe,不要直接双击nginx.exe,不要直接双击nginx.exe 一定要在dos窗口启动,不要直接双击nginx.exe,这样会导致修改配置后重启.停止nginx无效,需要手动关闭任务管理器内的所有nginx进程,再启动才可以 start

Nginx服务企业应用

1.1 常用来提供静态服务的软件 Apache :这是中小型Web服务的主流,Web服务器中的老大哥, Nginx :大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大. Nginx 的分支 Tengine ( http://tengine.taobao.org/)目前也在飞速发展• Lighttpd :这是一个不溫不火的优秀 Web软件,社区不活跃,静态解析效率很高.在 Nginx 流行前,它是大并发静态业务的首选,国内百度贴吧.豆瓣等众多网站都有Lighttpd奋斗的身彩" 1

nginx服务企业级应用

1.1 常用来提供静态服务的软件 Apache :这是中小型Web服务的主流,Web服务器中的老大哥, Nginx :大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大. Nginx 的分支 Tengine ( http://tengine.taobao.org/)目前也在飞速发展? Lighttpd :这是一个不溫不火的优秀 Web软件,社区不活跃,静态解析效率很高.在 Nginx 流行前,它是大并发静态业务的首选,国内百度贴吧.豆瓣等众多网站都有Lighttpd奋斗的身彩" 1

nginx服务器搭建

nginx服务搭建防火墙,安全机制的关闭iptables -F systemctl stop firewalldsetenforce 0本地yum的构建安装依赖包yum -y install pcre-devel zlib-devel openssl-develnginx服务的搭建需要pcre.zlib等软件包的技术支持从网上下载nginx软件包weget+复制的下载路径,或者下载到桌面rz上传tar -xf nginx-1.14.2.tar.gz -C /usr/src/ #把安装包解压到/u

nginx服务学习第二章

nginx.config文件中字符串不显示高亮 nginx服务搭建完成后,查看nginx.config的时候发现没有高亮字符,要想配置文件出现高亮方便观看,需要修改一些配置文件,修改步骤如下: # mkdir -p /root/.vim/syntax # cd  /root/.vim/syntax 从公网下载nginx.vim # wget https://www.vim.org/scripts/download_script.php?src_id=19394 -O nginx.vim 在vim

Linux网络服务--LAMP+Nginx+Squid搭建web集群

一.         项目名称         LAMP+Nginx+Squid搭建web集群环境 二.         项目拓扑 三.         项目描述 3.1 项目环境 某部队为了满足信息化政治工作建设需要,用以丰富官兵日常生活内容,活化教育形式,更好的建设部队人文环境,准备架设部队内部的网站服务器,并申请使用军内公网IP:1.1.1.1 基于以上情况为该部提出以下解决方案.   3.2 实施方案 3.2.1  服务器操作系统与软件版本选择 操作系统选择RedHat企业版5.10:

Tomcat集群+Nginx+Redis服务搭建

由于公司新业务突然上来了,单个Tomcat实例已经不能满足业务发展的需要了,只能通过搭建集群来解决问题了.所以就出现了下面的内容: 1.Redis保存Session信息 为了保存Session信息在集群中可用,所以session信息不能保存Tomcat中,因为如果是这样的话要想实现Session信息的共享就必须通过Session复制的方式来同步Tomcat之间的Session信息,这样比较费力费力而且性能消耗比较大,所以选择通过redis来实现Session信息的保存和共享. 1.1Redis的