Linux下Nginx下载安装

1.    安装
1)  rpm安装
rpm –ivh nginx
#这样安装可能会产生软件依赖问题
2)  yum 安装
yum install nginx –y
#简单,高效。但是不能定制,而且软件的更新不是很及时。而且先要安装epel源.
3)  编译安装
wget  + configure + make + make install
#可以定制软件,但是过程复杂,繁琐,效率低。
4)  定制化制作rpm包,搭建yum仓库。
 
安装nginx前,必须先安装一个pcre库
yum 安装 : yum install pcre –y
            yum install pcre-devel –y   #安装devel会自动安装pcre。
 
①    源码安装
wget http://nginx.org/download/nginx-1.8.1.tar.gz     #下载安装包
tar –xf nginx-1.8.1.tar.gz                                                         #解压安装包
cd nginx-1.8.1                                                                     #进入目录
useradd nginx –s /sbin/nologin -M                                        #添加用户和默认组
mkdir /usr/local/nginx-1.8.1                                            #创建安装目录
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.8.1    #安装nginx,如果想加模块,后面还可以连接模块,例如  --with-http_ssl_module,--with-http_stub_status_module等。这2个模块分别为:ssl功能,状态信息。
安装完毕后,创建一个无版本号的软链接
ln –s /usr/local/naginx-1.8.1 /usr/local/naginx
 
②yum安装
yum install epel –y
yum install nginx –y
 
2.    启动
①    源码安装
/usr/local/nginx/sbin/nginx
②    yum 安装
service nginx start
启动后检查
netstat –ntulp | grep nginx
lsof –i :80
ps –ef | grep nginx
 
3.    配置文件
  nginx的配置文件为: /usr/local/nginx/conf/nginx.conf
  主要是配置server里面的 
server {
        listen       80;                    #监听端口
        server_name  localhost;            #网站域名
        location / {
            root   html;                   #表示网站所放置的位置
            index  index.html index.htm;            #网站名
        }
        error_page   500 502 503 504  /50x.html;         #错误网站名
        location = /50x.html {
            root   html;
        }
 
 
4.    nginx模块

Nginx http 模块

Nginx http 功能模块

模块说明

 

 

ngx_http_core_module

包括一些核心的 http 参数配置,对应 Nginx 的配置为 HTTP 区块部分

ngx_http_access_module

访问控制模块,用来控制网站用户对 Nginx 的访问

ngx_http_gzip_module

压缩模块,对 Nginx 返回的数据压缩,属于性能优化模块

ngx_http_fastcgi_module

FastCGI 模块,和动态应用相关的模块,如 PHP

ngx_http_proxy_module

proxy 代理模块

ngx_http_upstream_module

负载均衡模块,可实现网站的负载均衡和节点的健康检查

ngx_http_rewrite_module

URL 地址重写模块

ngx_http_limit_conn_module

限制用户并发连接数以及请求数的模块

ngx_http_limit_req_module

根据定义的 key 限制 Nginx 请求过程的速率

ngx_http_log_module

访问日志模块,以指定的格式记录 Nginx 客户访问日志等信息

ngx_http_auth_basic_module

Web 认证模块,设置 Web 用户通过账号密码访问 Nginx

ngx_http_ssl_module

ssl 模块,用于加密的 http 连接,如 https

ngx_http_stub_status_module

记录 Nginx 基本访问状态信息等的模块

 
 
 
5.    多虚拟主机配置
1)    在nginx/conf文件夹中创建一个vhost文件夹
mkdir /usr/local/nginx/conf/vhost
2)    备份/usr/local/nginx/conf/nginx.conf
cp /usr/local/nginx/conf/nginx.conf{,.backup}
3)    修改nginx.conf文件
将文件中,server这一段删除,并添加
include vhost/www.conf
include vhost/bbs.conf
include vhost/blog.conf
或者 include vhost/*.conf
4)    从nginx.conf.backup文件中截取上面删除的server配置段,并在vhost中创建配置文件。
sed –n ’84,93’p /usr/local/nginx/conf/nginx.conf.backup >vhost/www.conf
sed –n ’84,93’p /usr/local/nginx/conf/nginx.conf.backup >vhost/bbs.conf
sed –n ’84,93’p /usr/local/nginx/conf/nginx.conf.backup >vhost/blog.conf
5)    去掉配置文件开头的#,编辑配置文件
vim host/www.conf       # server_name 设置为www.com,修改index.html为www.html
vim host/bbs.conf              # server_name 设置为bbs.com,修改index.html为bbs.html
vim host/blog.conf            # server_name 设置为blog.com,修改index.html为blog.html
6)    创建测试网页文件并测试配置文件的正确性
echo “www”> /usr/local/nginx/html/www.html
echo “bbs”> /usr/local/nginx/html/bbs.html
echo “blog”> /usr/local/nginx/html/blog.html
/usr/local/nginx/sbin/nginx –t
 
7)    查看nginx是否已经启动
ps –ef | grep nginx
lsof –i :80
如果没有启动,则:
/usr/local/nginx/sbin/nginx
如果已经启动,则:
/usr/local/nginx/sbin/nginx –s reload   #平滑重启
8)    测试网站搭建是否正确
修改C:\Windows\System32\drivers\etc\hosts,在最后面添加域名重定向。比如:
10.0.0.8        www.com
10.0.0.8       bbs.com
10.0.0.8       blog.com
9)    在浏览器中输入https://www.com  https://bbs.com  https://blog.com
 
 
6.    查看nginx运行状态
要查看状态,安装时必须安装ngx_http_stub_status_module 模块。
创建一个虚拟主机,并在主机中增加2
stub_status on;
access_log off';
可以追加访问控制
allow 10.0.0.2/24;
deny all;    #表示只运行这个IP来查看
 
7.    日志切割
创建一个脚本
vim cut_accesslog.sh
然后在脚本里面写入
cd /usr/local/nginx/logs/
/bin/mv /usr/local/nginx/logs/access.log access_$(date +%F).log
/usr/local/nginx/sbin/nginx –s reload
然后全路径加入到定时任务crontab
 
8.    location匹配
1) 优先匹配=
2) 其次匹配^~
3) 匹配正则表达式
4) 匹配普通字符串
5) 所有不能匹配到的,匹配到/上。
 
9.    rewrite 地址跳转
nginx通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。另外该模块需要PCRE支持,应在编译nginx时指定PCRE支持。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后nginx将返回500错误。同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等

 

1

2

3

4

5

6

7

8

9

10

11

.     :匹配除换行符以外的任意字符

?     :重复0次或1

+     :重复1次或更多次

*     :重复0次或更多次

\d    :匹配数字

^     :匹配字符串的开始字符

$     :匹配字符串的结束字符

{n}   :重复n

{n,}  :重复n次或更多次

[c]   :匹配单个字符c

[a-z] :匹配a-z小写字母的任意一个

 
rewrite 中的小括号和sed中的一样,后面可以接$1,$2….
例子:如果以前的域名为bbs.com,后来更改了域名为blog.com,但是想要用bbs.com也能到新的域名上,就用rewrite。
在bbs.conf中去除网页的定义。增加一行
rewrite ^/(.*) http://blog.com;
 
10.  ngx_http_auth_basic_module认证模块
ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容
语法:     auth_basic_user_file file;
默认值:     —
配置段:     http, server, location, limit_except
在配置文件中增加ngx_http_auth_basic_module模块。添加2行即可
auth_basic "test";
        auth_basic_user_file /usr/local/nginx/conf/vhost/passwd;
然后到指定的目录创建密码文件,注:密码必须是加密的。
echo "xiao:$(openssl passwd -crypt 123456)" > /usr/local/nginx/conf/vhost/passwd

猜你喜欢

转载自blog.csdn.net/freshair_x/article/details/80402825