博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Centos6.6下安装nginx1.6.3
阅读量:4507 次
发布时间:2019-06-08

本文共 11661 字,大约阅读时间需要 38 分钟。

安装环境:

[root@nginx ~]# cat /etc/redhat-release CentOS release 6.6 (Final)[root@nginx ~]# uname -r2.6.32-504.el6.x86_64

Nginx安装

1:安装必须的包

[root@nginx ~]# yum install -y pcre pcre-devel openssl-devel

pcre全称(Perl Compatible Regular Expressions),中文perl兼容正则表达式!(nginxrewite模块需要它)

2:添加虚拟用户

[root@nginx tools]# useradd -s /sbin/nologin -M nginx[root@nginx tools]# id nginxuid=501(nginx) gid=501(nginx) groups=501(nginx)

3:新建一目录,用于安装nginx

[root@nginx nginx-1.6.3]# mkdir /application

4:下载nginx

wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

百度下载:

微云下载:

5:编译安装nginx

wget -q http://nginx.org/download/nginx-1.6.3.tar.gztar zxf nginx-1.6.3.tar.gz./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_modulemake        #只要结尾没有报错就OKmake install

6:创建软链接

[root@nginx application]# ln -s nginx-1.6.3 nginx[root@nginx application]# lltotal 4lrwxrwxrwx 1 root root   11 Feb 14 21:31 nginx -> nginx-1.6.3drwxr-xr-x 6 root root 4096 Feb 14 21:30 nginx-1.6.3

7:启动nginx

[root@nginx application]# /application/nginx/sbin/nginx

查看是否启动成功:

[root@nginx application]# ps -ef|grep nginxroot      5205     1  0 21:32 ?        00:00:00 nginx: master process /application/nginx/sbin/nginxnginx     5206  5205  0 21:32 ?        00:00:00 nginx: worker process        root      5208   967  0 21:32 pts/0    00:00:00 grep nginx

查看nginx端口号:

[root@nginx application]# ss -lnput|grep nginxtcp    LISTEN     0      511                    *:80                    *:*      users:(("nginx",5205,6),("nginx",5206,6))

停掉nginx:

[root@nginx application]# /application/nginx/sbin/nginx -s stop

重启nginx:

[root@nginx application]# /application/nginx/sbin/nginx -s reload

以上就是nginx1.6.3的安装!此时在windonws浏览器下输入IP地址,即可看到如下页面:

Linux本机测试:

[root@nginx application]# curl 192.168.17.15Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.

Commercial support is available atnginx.com.

Thank you for using nginx.

nginx常用模块介绍

nginx技术内幕课程视频:

nginx核心模块:

Nginx配置文件(nginx.conf)配置详解

[root@ClientA nginx]# cat conf/nginx.conf#user  nobody;								#Nginx用户及组:用户 组。window下不指定worker_processes  1;							#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;					#pid(进程标识符):存放路径。events {    worker_connections  1024;}#每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为:worker_processes*worker_connectionshttp {    include       mime.types;			#设定mime类型,类型由mime.type文件定义    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;    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

基于域名的虚拟主机配置步骤

为了让配置文件规范化,我们使用一个网站一个配置文件,操作步骤如下:

1:编辑nginx.conf配置文件

[root@nginx application]# vim /application/nginx/conf/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    # nginx vhosts config    include extra/www.conf;    #include extra/bbs.conf;    #include extra/blog.conf;      ##如果有多个网站在这里添加配置文件路径                      }##编辑后的文件,删除了注释,添加include几行

2:在/application/nginx/conf/extra目录下创建对应的网站配置文件

[root@nginx conf]# mkdir extra[root@nginx conf]# cd extra[root@nginx extra]# cat www.confserver {    listen  80;    server_name www.ginvip.com alias ginvip.com;    location / {        root    html/ginvip;        index   index.html index.htm;    }}

上面的alias ginvip.com就是别名配置!类似于IIS里的301重定向

3:创建对应的网站目录(路径就是上面的配置文件中:html/ginvip)

[root@nginx nginx]# mkdir html/ginvip

4:创建测试文件

[root@nginx nginx]# cd html/ginvip[root@nginx ginvip]# lltotal 0[root@nginx ginvip]# echo "this is a test nginx website" >index.html[root@nginx ginvip]# lltotal 4-rw-r--r-- 1 root root 29 Feb 14 22:07 index.html

5:检查语法,重启nginx

[root@nginx ginvip]# /application/nginx/sbin/nginx -t      nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful[root@nginx ginvip]# /application/nginx/sbin/nginx -s reload

6:添加IP,域名到本地的hosts文件

Linux平台添加:

[root@nginx ginvip]# vim /etc/hosts192.168.17.15   www.ginvip.com ginvip.com

windows平台添加:(路径:C:\Windows\System32\drivers\etc\hosts)

7:测试

Linux平台测试:

[root@nginx ginvip]# curl www.ginvip.comthis is a test nginx website

测试别名:

[root@nginx ginvip]# curl ginvip.comthis is a test nginx website

windows平台测试:

基于端口的虚拟主机配置

基于端口的虚拟主机在生产环境中不多见,仅偶尔会用到,一般是为公司内部人员提供访问的,如OA系统,网站程序的后台,CMS发布后台,Mysqlweb客户端phpmyadmin等。

配置文件中的配置与基于域名的配置大致一样,只是修改上面配置文件中的80端口为你想要访问的端口!测试访问的时候,域名后面加上 :端口号

基于IP的虚拟主机实战

只需要在配置文件中的80端口前加上IP地址即可!

Nginx状态信息功能实战

1:在extra目录增加一个status.conf配置文件,内容如下:

[root@nginx nginx]# vim conf/extra/status.conf## website status server {    listen  80;    server_name status.ginvip.com;    location / {        stub_status on;  #开启http_stub_status_module模块        access_log  off;    }}

2:在主配置文件包含status.conf配置文件

[root@nginx nginx]# cat conf/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    # nginx vhosts config    include extra/www.conf;    include extra/status.conf;}

3:检查语法,重启nginx

[root@nginx nginx]# sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful[root@nginx nginx]# sbin/nginx -s reload

4:添加域名到/etc/hosts文件

5:测试status.ginvip.com

[root@nginx nginx]# curl status.ginvip.comActive connections: 1      #活跃的连接数量server accepts handled requests 4      4        4       4 Reading: 0 Writing: 1 Waiting: 0

第一个server表示Nginx启动到现在共处理了4个连接

第三个accepts表示Nginx启动到现在共成功创建了4次握手:请求丢失数=(握手数-连接数),可以看出,本次状态显示没有丢失请求

第三个handled requests,表示总共处理了4次请求

Reading: 0 Writing: 1 Waiting: 0

ReadingNginx读取到客户端的header信息数

WritingNginx返回给客户端的header信息数

WaitingNginx已经处理完正在等候下一次请求指令的驻留连接。开启keep-alive的情况下,这个值等于active-(reading+writing)

Nginx增加错误日志(error_log)配置

error_log的语法格式及参数语法说明如下:

error_log		file				level;#关键字		    日志文件	            错误日志级别

生产场景一般是warn|error|crit这三个级别之一,注意不要配置info等较低级别,会带来更多的磁盘I/O消耗

error_log的默认值为:

#default:	error_log	logs/error.log	error;

可以放置的标签段为:

#context:	main,	http,	server,	location

官方参考资料:

在主配置文件nginx.conf中添加日志项:

[root@nginx nginx]# cat conf/nginx.confworker_processes  1;error_log   logs/error.log  error;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    # nginx vhosts config    include extra/www.conf;    include extra/status.conf;}

Nginx访问日志介绍

Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供都分析用户浏览器行为等,此功能由ngx_http_log_module模块负责。

对应官方地址:

Nginx的访问日志主要由下面的两个参数控制:

参数

说明

log_format

用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)

access_log

用来指定日志文件的路径及使用的何种日志格式记录日志

Nginx日志格式中默认的参数配置格如下:

[root@ClientA nginx]# cat conf/nginx.conf.default    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';

将上面三行代码去掉#号,复制到主配置文件nginx.conf

[root@nginx nginx]# cat conf/nginx.confworker_processes  1;error_log   logs/error.log  error;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    # nginx vhosts config    include extra/www.conf;    include extra/status.conf;}

Nginx日志变量说明如下:

Nginx日志变量

说明

$remote_addr

记录访问网站的客户端地址

$http_x_forwarded_for

当前端有代理服务器时,设置WEB节点记录客户端地址的配置,此参数生效的前提是代理服务器上也要进行相关的x_forwarded_for设置

$remote_user

远程客户端用户名称

$time_local

记录访问时间与时区

$request

用户的http请求起始行信息

$status

http状态码,记录请求返回的状态,如:200 , 404 , 301

$body_bytes_sents

服务器发送给客户端的响应body字节数

$http_referer

记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置

$http_user_agent

记录客户端访问信息,如:浏览器,手机客户端

没有特殊要求,默认的配置即可,更多可以设置的记录日志信息变量见:

记录日志的access_log参数说明

access_log  off;	#这里的off,表示不记录访问日志

默认配置:access_log logs/access.log combined;

放置位置:http , server , location , if in location , limit_except

访问日志最好在单独的配置文件中配置:

[root@nginx nginx]# cat conf/extra/www.conf    server {    listen  80;    server_name www.ginvip.com alias ginvip.com;    location / {        root    html/ginvip;        index   index.html index.htm;    }    access_log  logs/access_www.log main;}

如果不指定日志格式就会用默认的combined格式记录日志

也可以对日志进行压缩,可以在记录日志参数中加上bufferflush选项,这样做会在高并发场景下提升网站访问....

access_log	logs/access_www.log	main gzip buffer=32k flush=5s;

更多内容参考:

Nginx访问日志轮询切割

默认情况nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,如果自己配置了日志,如上面我就对网站做了配置,写到了logs/access_www.conf文件里,时间长了会导致日志容量过大,不利于分析日志和处理,因此,有必要对Nginx按天或按指定时间进行切割成不同的文件保留,这里使用按天切割的方法:

[root@nginx scripts]# cat cut_nginx_log.sh#!/bin/shDateformat=`date +%Y%m%d -d -1day`Basedir='/application/nginx'Nginxlogdir="$Basedir/logs"Logname="access_www"[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1[ -f ${Logname}.log ] || exit 1/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log$Basedir/sbin/nginx -s reload

将此脚本加入到定时任务中即可!

Nginx常用日志收集及分析工具

见:

 

转载于:https://www.cnblogs.com/ginvip/p/6399306.html

你可能感兴趣的文章
用Swift重写公司OC项目(Day1)--程序的AppIcon与LaunchImage如何设置
查看>>
C++ 小的使用技巧和注意点
查看>>
x项目第一周随笔
查看>>
智能资产构建去中心化的资产管理系统
查看>>
[JavaWeb基础] 003.JAVA访问Mysql数据库
查看>>
BZOJ 1045 糖果传递(思维)
查看>>
区块链北大课程总结(课程2-数据结构)
查看>>
图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
查看>>
JSP的页面连接和提交方式(web基础学习笔记六)
查看>>
HTTP首部
查看>>
python 基础 列表生成式 生成器
查看>>
Linux unalias命令 取消别名
查看>>
LoadRunner
查看>>
Ubuntu 部署Python开发环境
查看>>
微信小程序——获取步数
查看>>
代理原有的Handler.Callback,感知Application onCreate的结束时间
查看>>
Delphi 皮肤控件AlphaControls的使用
查看>>
pycharm快捷键大全
查看>>
Smarty 简单使用
查看>>
冒泡排序
查看>>