Nginx安装与优化

Nginx安装

1、下载依赖包
yum install pcre pcre-devel -y
yum install openssl-devel –y

2、建立nginx用户
useradd nginx -s /sbin/nologin –M

3、创建app目录
mkdir /application

4、创建下载点目录
mkdir /home/oldboy/tools –p

5、切换目录并下载
cd /home/oldboy/tools/
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

6、解压
tar xf nginx-1.6.3.tar.gz

7.编译安装nginx
cd nginx-1.6.3
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

8、使其生效
make
make install

9、创建软链接
ln -s /application/nginx-1.6.3/ /application/nginx

10、启动nginx
/application/nginx/sbin/nginx

11、检查有没有nginx进程
ps -ef |grep nginx|grep -v grep

配置站点
[[email protected] nginx]# ls -l|grep -v temp
total 36
drwxr-xr-x 2 root  root 4096 May 11 16:02 conf    #配置文件目录
drwxr-xr-x 2 root  root 4096 May 11 16:02 html    #站点(默认网站目录)
drwxr-xr-x 2 root  root 4096 May 11 16:13 logs    #错误、访问日志、
drwxr-xr-x 2 root  root 4096 May 11 16:02 sbin    #启动命令

cd /html
vim index.html
<html>
<head><title>oldboy,s Nginx server blog.</title></head>
<body>
Hi, I am oldboy. My blog address is
<a href="http://oldboy.blog.51cto.com">http://oldboy.blog.51cto.com
</body>
</html>

基于域名的虚拟主机配置

一:命令历史记录

切换到配置文件目录
cd conf/

查看包含目录
ls -l nginx.conf
操作前备份
cp nginx.conf{,.ori}

去掉配置文件中无用东西
egrep -v "#|^$" nginx.conf.default >nginx.conf

编辑配置文件
vim nginx.conf

操作完检查
cat nginx.conf

创建站点目录
mkdir ../html/{www,bbs} –p

操作完检查
tree ../html/

追加一些内容到站点目录
echo "www.etiantian.org" > ../html/www/index.html
echo "bbs.etiantian.org" > ../html/bbs/index.html

操作完检查
cat ../html/{www,bbs}/index.html

检查语法
/application/nginx/sbin/nginx –t

优雅重启
/application/nginx/sbin/nginx -s reload

编辑本地hosts文件
vim /etc/hosts

ping看是否返回本机IP
ping bbs.etiantian.org

curl访问是否成功
curl www.etiantian.org
curl bbs.etiantian.org

二:具体演示
[[email protected] conf]# cat 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;
            index  index.html index.htm;
        }
    } 
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
}

创建站点目录

[[email protected] conf]# mkdir ../html/{www,bbs} -p
[[email protected] conf]# tree ../html/
../html/
├── 50x.html
├── bbs
├── index.html
└── www

2 directories, 2 fil

[[email protected] conf]# echo "www.etiantian.org" > ../html/www/index.html
[[email protected] conf]# echo "bbs.etiantian.org" > ../html/bbs/index.html 
[[email protected] conf]# cat ../html/{www,bbs}/index.html                
www.etiantian.org
bbs.etiantian.org

检查语法
[[email protected] conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

重新加载
[[email protected] conf]# /application/nginx/sbin/nginx -s reload

增加域名解析
[[email protected] conf]# vim /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org

ping检查返回值是否是对应IP
[[email protected] conf]# ping www.etiantian.org
PING www.etiantian.org (10.0.0.7) 56(84) bytes of data.
64 bytes from www.etiantian.org (10.0.0.7): icmp_seq=1 ttl=64 time=0.020 ms
################
[[email protected] conf]# ping bbs.etiantian.org

访问
[[email protected] conf]# curl www.etiantian.org
www.etiantian.org
[[email protected] conf]# curl bbs.etiantian.org
bbs.etiantian.org

到此,基于域名的虚拟主机就配好了

---------------------------------------
经验总结:
[[email protected] conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 www.etiantian.org bbs.etiantian.org

在工作中只需在DNS购买的管理界面里将域名解析成A记录 ,将两域名以A记录的形式解析到公网IP。
windows中:WIN+R  输入drivers  编辑\etc\hosts文件 把解析写进去

基于域名的虚拟主机访问原理
HTTP访问原理
浏览器输入www.etiantian.org。
在请求报文的请求头会有一行[host:www.etiantian.org]字段。
表示客户端告诉服务器,想要这个主机的内容
内容到达服务器后
服务器阅读请求报文,就会返回对应的响应报文。
如果没有带host请求头,默认会返回配置文件中第一个域名

=============================================================

实战:增加blog.etiantian.org ============>html/blog,浏览域名显示blog.etiantian.org
a、增加默认配置  /application/nginx/conf
    server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }

b、创建站点目录
[[email protected] conf]# mkdir ../html/blog -p
[[email protected] conf]# tree ../html/
../html/
├── 50x.html
├── bbs
│?? └── index.html
├── blog
├── index.html
└── www
    └── index.html

c、首页
[[email protected] conf]# echo "blog.etiantian.org" > ../html/blog/index.html 
[[email protected] conf]# cat ../html/{www,bbs,blog}/index.html
www.etiantian.org
bbs.etiantian.org
blog.etiantian.org

d、检查配置文件语法
[[email protected] conf]# /application/nginx/sbin/nginx -t

e、修改了配置文件,需要优雅重启
/application/nginx/sbin/nginx -s reload

f、域名解析
[[email protected] conf]# vim /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org

g、检查返回是否是自己主机IP
[[email protected] conf]# ping blog.etiantian.org
PING www.etiantian.org (10.0.0.7) 56(84) bytes of data.
64 bytes from www.etiantian.org (10.0.0.7): icmp_seq=1 ttl=64 time=0.028 ms

h、进行访问
[[email protected] conf]# curl blog.etiantian.org
blog.etiantian.org

i、windows域名解析
将10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org放进C:\Windows\System32\drivers\etc\hosts文件中

j、浏览器进行访问
浏览器访问成功,并显示:blog.etiantian.org
========================================
基于端口的Nginx
a、修改默认配置
[[email protected] conf]# cat 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       8001;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    } 
    server {
        listen       8002;
        server_name  www.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       8003;
        server_name  www.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

b、检查语法
[[email protected] conf]# /application/nginx/sbin/nginx -t

c、优雅重启
[[email protected] conf]# ../sbin/nginx -s reload

d、进行访问
[[email protected] conf]# curl http://www.etiantian.org:8001
www.etiantian.org
[[email protected] conf]# curl http://www.etiantian.org:8002
bbs.etiantian.org
[[email protected] conf]# curl http://www.etiantian.org:8003
blog.etiantian.org

=======================================================

基于IP的虚拟主机很少用到就不做赘述

Nginx优化

Nginx常用选项:
  ? - - H:此帮助
  -v:显示版本并退出
  -V:显示版本和配置选项,然后退出
  -t:测试配置和退出
  Q:抑制配置测试在非错误信息
  -s信号:将信号发送到主进程:停止,退出,重新打开,重装
  -p前缀:设置前缀路径(默认:/application/nginx-1.6.3/)
  -c文件名:设置配置文件(默认:CONF / nginx.conf)
  -g指令:设置全局指令进行配置文件

优化一:优化配置文件

1、规范优化Nginx配置文件
[[email protected] conf]# cp nginx.conf{,ori.1}
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# cat nginx.conf
worker_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;
}

2、创建extra目录
[[email protected] conf]# mkdir extra
a、
[[email protected] conf]# sed -n ‘10,17p‘ nginx.conf.base-name
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    } 
[[email protected] conf]# sed -n ‘10,17p‘ nginx.conf.base-name >extra/www.conf
[[email protected] conf]# cat extra/www.conf

b、
[[email protected] conf]# sed -n ‘18,25p‘ nginx.conf.base-name
[[email protected] conf]# sed -n ‘18,25p‘ nginx.conf.base-name >extra/bbs.con
[[email protected] conf]# cat extra/bbs.conf

c、
[[email protected] conf]# sed -n ‘26,33p‘ nginx.conf.base-name
[[email protected] conf]# sed -n ‘26,33p‘ nginx.conf.base-name >extra/blog.con
[[email protected] conf]# cat extra/blog.conf

3、检查语法
[[email protected] conf]# ../sbin/nginx -t

4、重启
[[email protected] conf]# ../sbin/nginx -s reload

5、查看本地/etc/hosts里面是否有域名解析
[[email protected] conf]# cat /etc/hosts
如果没有的话,加上域名解析
[[email protected] conf]# vim /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org

6、检查
[[email protected] conf]# curl www.etiantian.org
www.etiantian.org
[[email protected] conf]# curl bbs.etiantian.org
bbs.etiantian.org
[[email protected] conf]# curl blog.etiantian.org
blog.etiantian.org

优化二:别名设置
1、修改配置
[[email protected] conf]# vi extra/www.conf
        server_name  www.etiantian.org;                #修改前
        server_name  www.etiantian.org etiantian.org;    #修改后
2、增加域名解析
[[email protected] conf]# vim /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org                    #修改前
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org   #修改后
3、访问检查
[[email protected] conf]# curl etiantian.org
www.etiantian.org
4、总结:利用别名可以少一次请求,不用跳转
[[email protected] conf]# curl -I 51cto.com
HTTP/1.1 301 Moved Permanently
[[email protected] conf]# curl -I etiantian.org
HTTP/1.1 200 OK
[[email protected] conf]# curl -I baidu.com
HTTP/1.1 200 OK

优化三:配置Nginx status
1、
cat >>/application/nginx/conf/extra/status.conf<<EOF
###status
    server {
        listen       80;
        server_name  status.etiantian.org;
        location / {
            stub_status on;
            access_log   off;
        }
    } 
EOF

2、增加包含
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# cat nginx.conf
worker_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 extra/status.conf;
}
3、检查语法
[[email protected] conf]# /application/nginx/sbin/nginx -t

4、重启
[[email protected] conf]# ../sbin/nginx -s reload

5、增加域名解析
[[email protected] conf]# vim /etc/hosts
[[email protected] conf]# cat /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org status.etiantian.org

6、检查访问
[[email protected] conf]# curl status.etiantian.org
Active connections: 1                 #正在处理的活动连接数
server accepts handled requests       
54 54 41
Reading: 0 Writing: 1 Waiting: 0

Active connections #正在处理的活动连接数
server:表示Nginx启动到现在共处理了N个连接
accepts:表示Nginx启动到现在共成功创建了N次握手
handled requests :表示总共处理了N次请求

Reading:读取到客户端的Header信息数
Writing:返回给客户端的Header信息数
Waiting:已经处理完等待下一次请求指令的驻留连接。

优化四:错误日志
添加到main全局配置  也可以添加到server下,每台主机有自己的错误日志
[[email protected] conf]# vim nginx.conf
将error_log logs/error.log error;添加到worker下面
[[email protected] conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log error;

2、查看错误日志
[[email protected] conf]# cat ../logs/error.log
如果日志太多,可以先清空,再调试
[[email protected] conf]# >../logs/error.log
[[email protected] conf]# ../sbin/nginx -s reload
[[email protected] conf]# cat ../logs/error.log
2016/05/12 19:08:06 [notice] 12914#0: signal process started

访问日志
[[email protected] conf]# sed -n ‘21,23p‘ 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"‘;
把#号去掉放进配置文件里
[[email protected] conf]# cat nginx.conf
worker_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/bbs.conf;
    include extra/blog.conf;
    include extra/status.conf;
}

优化五:访问日志
最好基于虚拟主机
[[email protected] conf]# vim extra/www.conf
[[email protected] conf]# cat extra/www.conf   
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        access_log logs/access_www.log main;  #新增行
    }

查看
[[email protected] conf]# tail -f ../logs/access_www.log
10.0.0.1 - - [12/May/2016:21:02:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36" "-"
10.0.0.1 - - [12/May/2016:21:02:18 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "http://10.0.0.7/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36" "-"

时间: 2024-08-27 01:29:57

Nginx安装与优化的相关文章

编译安装nginx及简单优化配置

一.背景 使用源码包安装lnmp架构及简单的优化配置 二.实验环境 rhel6.5 三.安装过程 1.nginx(提前装好gcc等编译器) (1) 下载源码包  http://nginx.org/ (2) tar -zxf nginx-1.8.1.tar.gz (3) cd nginx-1.8.1 vim auto/cc/gcc 修改第179行 (将本行注释,意为取消debug模式,) (4) ./configure --prefix=/usr/local/lnmp/nginx \ --with

nginx运维实战之开山篇-安装时优化实战

本次博文议程如下: 1.1 NGINX安装和基本优化 测试环境:centos6.5 x64 ip 192.168.1.62 安装前基本优化详见 1.1.1 隐藏版本 为了防止被黑客扫描到web服务器信息,通过相对应的web服务器信息找出对应的版本漏洞,从而对web服务器进行入侵,nginx虽然功能强大,但是也是软件,软件就可能会有漏洞,例如nginx-0.6.32版本,默认情况下可能导致服务器错误的将任何类型的文件以php的方式进行解析,比如上传一个jpg格式的木马到论坛网站,通过漏洞解析成一个

nginx安装及配置优化

安装环境:centos6.5 64位 nginx版本:nginx-1.4.7 一.安装 安装必要软件 pcre和openssl 为了支持rewrite功能,我们需要安装pcre tar zxvf pcre-8.31.tar.gz cd pcre-8.31 ./configure && make&& make install 为了ssl支持,需要安装openssl yum -y install openssl* 2.安装nginx 解压: tar -zxvf nginx-1.

LNMP环境之Nginx/Tengine的源代码安装及优化

一.介绍: Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验.它的最终目标是打造一个高效.稳定.安全.易用的Web平台. 二.下载并安装: [[email protected] ]#wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz 安装前需要安装依赖的库文件: yum  i

Nextcloud 安装与优化 centos7 php7 nginx MariaDB

Nextcloud 安装与优化 (环境: centos7 php7 nginx MariaDB)前期玩了一段时间这个,但当时都是基于宝塔面板的,很多东西都不知道是为了什么,现在根据前期各位大神的作品,从头到尾做了一次.发现对于我们小白来说问题不在于安装,而在于优化,而大神们将优化说的比较少,所以我将自己的过程记录下:一.准备工作:1.查看系统版本并升级cat /etc/redhat-releaseyum update -y 2.将自带的epel.nginx.php全部卸载rpm -qa|grep

Nginx负载均衡优化插件编译及配置

一. Ngix依赖模块安装 Ngix依赖模块有:pcre.zlib.openssl.md5 /sha1(如果系统中没有安装相应模块,需要按照下列方式安装) 1. 安装pcre模块(8.35) 官方网站:http://www.pcre.org/ 安装命令: # unzip pcre-8.35.zip # cd pcre-8.35 # ./configure # make && make install 在64位linux系统中,nginx搜索的库位置为lib64:所以,需要建立软连接: #

linux下nginx安装、配置实战

1什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apache服务器不错的替代品.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.目前中国大陆使用nginx网站用户有:新浪.网易.腾讯,另外知名的微网志Plurk也使用nginx. Nginx作为负载均衡服务器,既可以在内部直接支持Rails和PHP程序对外进行服务,也可

linux下nginx的安全优化

上节我们说了Apache的Web服务安全与优化.分别说了进程优化,版本号的隐藏,会话连接的时间,DNS查询.我们只要掌握这些优化点就够了,大家不要一味追求求精,什么都有个度的,你弄的太过了,相应他别的方面也就会不行了,所以我们优化的标准是找一个折衷点,是最好的!接下来我们说下nginx的web服务的安全优化. Nginx的安全优化原理跟Apache大致一样的,有一些细微的差别,和更改参数的地方不同而已! 在说优化nginx之前我们先了解下他的结构: Nginx由内核和模块组成,其中,内核的设计非

【nginx】配置优化

1.编译安装过程优化 在编译Nginx时,默认以debug模式进行,而在debug模式下会插入很多跟踪和ASSERT之类的信息,编译完成后,一个Nginx要有好几兆字节.在编译前取消Nginx的debug模式,编译完成后Nginx只有几百千字节,因此可以在编译之前,修改相关源码,取消debug模式,具体方法如下:在Nginx源码文件被解压后,找到源码目录下的auto/cc/gcc文件,修改如下几行 sed -i '[email protected]="$CFLAGS -g"@#CFLA