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

[经验分享] nginx作为web服务器

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-1 09:06:25 | 显示全部楼层 |阅读模式
特性:
·模块化设计
·nginx热部署(不需要中断正在处理的请求,更新配置)
·3xx-5xx错误重定向
·重写(rewrite)模块
·根据浏览器的类型返回哪个页面(比如手机、平板)
·支持验证Referer验证(防盗链)
·支持FLV流和MP4流(下载视频的过程中能一边一下载一边播放)
优点:
·支持kqueuq、epoll等IO模型(异步IO),由此来支持高并发
·内存消耗小
应用场合:
·nginx结合FastCGI运行PHP.JSP,Perl等程序
·nginx作反向代理(http、mail)
·nginx运行静态HTML页面
为什么nginx处理静态小文件能力强并发高?
答:使用epoll和kqueue I/O模型
静态业务:nginx
动态业务:apache
nginx工作模式:一个master后台N个worker(1个主进程多个worker进程)

安装Nginx:
1.安装pcre库:
·pcre是什么:perl兼容正表达式
·为什么安装pcre:使nginx支持HTTP rewrite模块
1
2
3
4
    tar zxvf  pcre-8.37.tar
    cd pcre-8.37
    ./configure
    make && make install



2.nginx参数
    /usr/local/nginx/sbin/nginx  -t         测试
    /usr/local/nginx/sbin/nginx  -s reload  平滑重启
    /usr/local/nginx/sbin/nginx  -V         查看怎么编译的
3.nginx路径
1
2
3
·配置文件路径:/uer/local/nginx/conf/nginx.conf
·启动文件路径:/uer/local/nginx/sbin/nginx
·网页文件路径:/uer/local/nginx/html



4.安装nginx

·关闭防火墙:       service iptables stop
·关闭SElinux:      setenforce 0
·添加nginx账户:    useradd nginx -s /sbin/nologin -M
·                   tar zxvf nginx-1.8.0.tar.gz
·                   cd nginx-1.8.0
· ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module
·                  make && make  install
·执行/usr/local/nginx/sbin/nginx -t 报错:error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
·解决:1.find / -name libpcre.so* 找到libpcre.so*的路径 在/usr/local/lib 下
        2.vim /etc/lb.so.conf 把/usr/local/lib添加进去
        3.ldconfig重新加载,OK
·启动nginx:/usr/local/nginx/sbin/nginx

5.nginx主配置文件详解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
·去掉注释: egrep -v  "#|^$"  nginx.conf
#user  nobody;                            #使用哪个账户
worker_processes  1;                      #启动的worker线程数

#error_log  logs/error.log;              
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;             #启动一个worker线程为1024
}


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;

    server {
        listen       80;                    #监听端口
        server_name  localhost;             #域名地址

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                        #uri,“/”表示默认路径/nginx/html下的页面
            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;   #50x错误时,跳转到50x.html
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {                 #以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;
    #    }
    #}
   ·zhaijunming.iyunv.com/5449012/1686898
     URL:zhaijunming.iyunv.com
     URI:/5449012/1686898
    location [= | ~ | ~* | ^~ ] uri {}
    location  =   uri {} 精确匹配
    location  ~   uri {} 区分大小写
    location  ~*  uri {} 不区分大小写
    location  ^~  uri {} 不实使用正则表达式



6.把主配置文件和虚拟主机配置文件分离
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
·主配置文件(全局)nginx.conf
worker_processes  1;                              
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    }
  include  extra/nginx_vhosts.conf;   #定义虚拟主机配置文件,/usr/local/nginx/conf/extra/nginx_vhosts.conf
·创建虚拟主机文件:mkdir -p /usr/local/nginx/conf/extra
                    touch    /usr/local/nginx/conf/extra/nginx_vhosts.conf
·虚拟主机配置文件
    vim /usr/local/nginx/conf/extra/nginx_vhosts.conf
  server    {
          listen       80;
          server_name                  
          location / {
            root   /data/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  blog.aaa.com;
          location / {
            root   /data/www/blog;
            index  index.html index.htm;
           }

          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
            root   html;
     }
   }




7.定义日志
·将这段加入到nginx.conf中的http {}中,表示为所有虚拟主机使用这种格式的日志
   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  /usr/local/nginx/logs/access.log  main;
·第一行main为定义的日志格式,最后一行main调用上面main的格式。
·也可以将最后一行加入到nginx_vhots.conf中的server{}中,表示为某个虚拟主机使用此格式


运维网声明 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-108154-1-1.html 上篇帖子: nginx+tomcat java报Broken pipe错误 下篇帖子: CentOS 7 + Nginx 1.9.4 web服务器 浏览器 下载视频 master 表达式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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