Nginx基础配置和使用,Nginx基础配置使用


内容摘要:

nginx 特点

nginx应用场合

nginx安装

nginx主配置文件

nginx基于域名的虚拟主机

nginx基于域名的端口主机

nginx基于域名的IP主机

nginx配置负载均衡

nginx加入service添加启动

nginx 特点

1、配置简单,灵活,轻量。

2、高并发(静态小文件),静态几万的并发。

3、占用资源少。2W并发 开10个线程服务,内存消耗几百M。

4、功能种类比较多(web,cache,proxy),每一个功能都不是特别强。

5、支持epoll模型。使得NGINX可以支持高并发!apache select 模型。

6、利用nginx 可以对IP限速,可以限制连接数。

7、nginx可以配合动态PHP(FASTCGI接口)

它所具备的其他WWW服务特性如下:

支持基于名字、端口以及IP的多虚拟主机站点:

支持rewrite模块,支持URI重写及正则表达式匹配:

支持http响应速率限制:

支持同一IP地址的并发连接或请求数限制

nginx应用场合

1、提供静态服务(图片,视频服务),另一个lighttpd。并发:几万并发

html,js,css,.flv,jpg,gif等,类似lighttpd。

2、提供动态服务,nginx+fastcgi的方式运行php,jsp。

动态并发:500-1500

3、提供反向代理(proxy)服务,或者称为负载均衡。日PV2000W 以下,并发1万以下,都可以直接用NGINX 做代理。

4、缓存服务,类似SQUID,VARNISH。

nginx 虚拟主机

一个server标签就是一个虚拟主机

1、基于域名的虚拟主机。通过域名来区分虚拟主机 ==》应用:外部网站

2、基于端口的虚拟主机。通过端口来区分虚拟主机 ==》应用:公司内部网站,网站的后台

3、基于IP的虚拟主机。几乎不用。不支持ifconfig别名,配置文件可以。

nginx安装

nginx网址 nginx.org
a、安装PCRE(Perl Compatible Regular Expressions),peri兼容正则表达式,官方站点为www.pcre.org
先查看服务器的版本
cat /etc/redhat-release
uname -m
查看是否有pcre
rpm -qa pcre pcre-devel
安装pcre
yum install pcre pcre-devel -y
  
b、SSL modules require the OpenSSL library
安装openssl
yum install openssl openssl-devel -y
  
c、安装nginx
创建虚拟用户
useradd nginx -s /sbin/nologin -M
解压文件
tar zxcf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --help 列出所有的可选项
./configure --prefix=/mnt/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
echo $? 判断是否成功
0
然后 make 编译安装
make
make install
  
启动nginx
/mnt/nginx-1.6.3/sbin/nginx
检查是否启动
ps -ef |grep nginx| grep -v grep 或者 ss -lntup | grep nginx
  
/mnt/nginx/sbin/nginx -V 来查看这个是如何编译安装的
  
部署一个简单的web站点
cd /mnt/nginx-1.6.3
ls -l | grep -v temp
drwxr-xr-x. 2 root root 4096 12月 9 10:19 conf 配置
drwxr-xr-x. 2 root root 4096 12月 9 10:19 html 默认网站
drwxr-xr-x. 2 root root 4096 12月 9 10:29 logs 错误,访问日志,PID
drwxr-xr-x. 2 root root 4096 12月 9 10:19 sbin 启动命令
cd html
vim index.html 随意修改后,保存
可以发现这个时候,nginx 默认首页已经变化了

nginx主配置文件

#user nobody;
worker_processes 1;
  
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
  
#pid logs/nginx.pid;
------------------------------------------ Main区,Nginx核心功能模块
  
events {
worker_connections 1024;
}
------------------------------------------ events区,Nginx核心功能模块
  
http {
include mime.types;
default_type application/octet-stream;
  
sendfile on; #高效文件传输
keepalive_timeout 65; #超时时间
  
#一个server标签就是一个虚拟主机
server {
listen 80;#监听的端口
server_name localhost;#域名
#所有的请求,执行的参数 location区块
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
#如果是下面的情况,优先执行下面
location = /50x.html {
root html;
}
}
}

nginx基于域名的虚拟主机

最小化配置文件,去掉#和空格

egrep -v "#|^$" nginx.conf.default >nginx.conf
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs; #目录为bbs
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
创建www和bbs的目录,分别设置站点
mkdir mnt/nginx/html/{www,bbs} -p
echo "www" > www/index.html
echo "bbs" > bbs/index.html
#检查语法是否OK
/mnt/nginx/sbin/nginx -t
hosts绑定本机IP后,测试成功
[root@localhost html]# curl www.etiantian.org
www
[root@localhost html]# curl bbs.etiantian.org
bbs

nginx基于端口的虚拟主机

修改server中的端口即可

server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8001;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

nginx基于IP的虚拟主机

用的少,这边不描述

nginx配置负载均衡

http {
    include       mime.types;
    default_type  application/octet-stream;
 
    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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
    upstream test.miaohr.com {
        server 118.31.239.163:80;
        server 118.31.239.163:8080;
    } 
 
    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://test.miaohr.com ;    
        #proxy_set_header  X-Real-IP  $remote_addr;    
        client_max_body_size  100m;        
        }

这边配置负载均衡池

upstream test.miaohr.com {
       server 118.31.239.163:80;
       server 118.31.239.163:8080;
   }  <br>后面写好地址即可

proxy_pass http://test.miaohr.com ;

相关内容

    暂无相关文章