设为首页 收藏本站
查看: 925|回复: 0

[经验分享] Nginx安装与优化

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-16 09:06:32 | 显示全部楼层 |阅读模式


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

配置站点
[iyunv@web02 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://www.iyunv.com">http://www.iyunv.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.iyunv.com" > ../html/www/index.html
echo "bbs.iyunv.com" > ../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.iyunv.com

curl访问是否成功
curl www.iyunv.com
curl bbs.iyunv.com


  二:具体演示
[iyunv@web02 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.iyunv.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  bbs.iyunv.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
}

创建站点目录

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

2 directories, 2 fil

[iyunv@web02 conf]# echo "www.iyunv.com" > ../html/www/index.html
[iyunv@web02 conf]# echo "bbs.iyunv.com" > ../html/bbs/index.html
[iyunv@web02 conf]# cat ../html/{www,bbs}/index.html               
www.iyunv.com
bbs.iyunv.com

检查语法
[iyunv@web02 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

重新加载
[iyunv@web02 conf]# /application/nginx/sbin/nginx -s reload

增加域名解析
[iyunv@web02 conf]# vim /etc/hosts
10.0.0.7 www.iyunv.com bbs.iyunv.com

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


访问
[iyunv@web02 conf]# curl www.iyunv.com
www.iyunv.com
[iyunv@web02 conf]# curl bbs.iyunv.com
bbs.iyunv.com

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

---------------------------------------
经验总结:
[iyunv@web02 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.iyunv.com bbs.iyunv.com

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

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

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

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

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

c、首页
[iyunv@web02 conf]# echo "blog.iyunv.com" > ../html/blog/index.html
[iyunv@web02 conf]# cat ../html/{www,bbs,blog}/index.html
www.iyunv.com
bbs.iyunv.com
blog.iyunv.com

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

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

f、域名解析
[iyunv@web02 conf]# vim /etc/hosts
10.0.0.7 www.iyunv.com bbs.iyunv.com blog.iyunv.com

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

h、进行访问
[iyunv@web02 conf]# curl blog.iyunv.com
blog.iyunv.com

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

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

b、检查语法
[iyunv@web02 conf]# /application/nginx/sbin/nginx -t

c、优雅重启
[iyunv@web02 conf]# ../sbin/nginx -s reload

d、进行访问
[iyunv@web02 conf]# curl http://www.iyunv.com:8001
www.iyunv.com
[iyunv@web02 conf]# curl http://www.iyunv.com:8002
bbs.iyunv.com
[iyunv@web02 conf]# curl http://www.iyunv.com:8003
blog.iyunv.com

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

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





Nginx优化

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

优化一:优化配置文件

1、规范优化Nginx配置文件
[iyunv@web02 conf]# cp nginx.conf{,ori.1}
[iyunv@web02 conf]# vim nginx.conf
[iyunv@web02 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目录
[iyunv@web02 conf]# mkdir extra
a、
[iyunv@web02 conf]# sed -n '10,17p' nginx.conf.base-name
    server {
        listen       80;
        server_name  www.iyunv.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
[iyunv@web02 conf]# sed -n '10,17p' nginx.conf.base-name >extra/www.conf
[iyunv@web02 conf]# cat extra/www.conf

b、
[iyunv@web02 conf]# sed -n '18,25p' nginx.conf.base-name
[iyunv@web02 conf]# sed -n '18,25p' nginx.conf.base-name >extra/bbs.con
[iyunv@web02 conf]# cat extra/bbs.conf

c、
[iyunv@web02 conf]# sed -n '26,33p' nginx.conf.base-name
[iyunv@web02 conf]# sed -n '26,33p' nginx.conf.base-name >extra/blog.con
[iyunv@web02 conf]# cat extra/blog.conf

3、检查语法
[iyunv@web02 conf]# ../sbin/nginx -t

4、重启
[iyunv@web02 conf]# ../sbin/nginx -s reload

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

6、检查
[iyunv@web02 conf]# curl www.iyunv.com
www.iyunv.com
[iyunv@web02 conf]# curl bbs.iyunv.com
bbs.iyunv.com
[iyunv@web02 conf]# curl blog.iyunv.com
blog.iyunv.com

优化二:别名设置
1、修改配置
[iyunv@web02 conf]# vi extra/www.conf
        server_name  www.iyunv.com;                #修改前
        server_name  www.iyunv.com iyunv.com;    #修改后
2、增加域名解析
[iyunv@web02 conf]# vim /etc/hosts
10.0.0.7 www.iyunv.com bbs.iyunv.com blog.iyunv.com                    #修改前
10.0.0.7 www.iyunv.com bbs.iyunv.com blog.iyunv.com iyunv.com   #修改后
3、访问检查
[iyunv@web02 conf]# curl iyunv.com
www.iyunv.com
4、总结:利用别名可以少一次请求,不用跳转
[iyunv@web02 conf]# curl -I iyunv.com
HTTP/1.1 301 Moved Permanently
[iyunv@web02 conf]# curl -I iyunv.org
HTTP/1.1 200 OK
[iyunv@web02 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.iyunv.com;
        location / {
            stub_status on;
            access_log   off;
        }
    }
EOF

2、增加包含
[iyunv@web02 conf]# vim nginx.conf
[iyunv@web02 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、检查语法
[iyunv@web02 conf]# /application/nginx/sbin/nginx -t

4、重启
[iyunv@web02 conf]# ../sbin/nginx -s reload

5、增加域名解析
[iyunv@web02 conf]# vim /etc/hosts
[iyunv@web02 conf]# cat /etc/hosts
10.0.0.7 www.iyunv.com bbs.iyunv.com blog.iyunv.com iyunv.com status.iyunv.com

6、检查访问
[iyunv@web02 conf]# curl status.iyunv.com
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下,每台主机有自己的错误日志
[iyunv@web02 conf]# vim nginx.conf
将error_log logs/error.log error;添加到worker下面
[iyunv@web02 conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log error;

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


访问日志
[iyunv@web02 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"';
把#号去掉放进配置文件里
[iyunv@web02 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;
}



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

查看
[iyunv@web02 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" "-"



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-217527-1-1.html 上篇帖子: Node.js + Nginx搭建基于websocket的、可扩展的消息中心 下篇帖子: linux企业常用服务---部署Nginx+Tomcat负载均衡集群 用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表