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

[经验分享] 源码安装Nginx及配置文件详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-9 08:37:16 | 显示全部楼层 |阅读模式
一、安装Nginx
二、Nginx编译选项
三、Nginx进程管理命令
四、Nginx配置文件解析


一、安装Nginx
1、提前安装所需软件依赖包
?
1
yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel perl perl-ExtUtils-Embed



2、下载Nginx源码安装包
?
1
wget http://nginx.org/download/nginx-1.4.0.tar.gz



3、解压编译(禁用模块使用参数--without,编译第三方模块使用参数--add-module=/path/module)
?
1
tar -xzf nginx-1.4.0.tar.gz -C /usr/src/



?
1
2
3
4
5
6
7
8
9
10
11
12
13
cd /usr/src/nginx-1.4.0/
./configure --prefix=/usr/local/nginx \
--with-ipv6 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module



4、安装
?
1
make && make install




安装完成后,程序主目录位于/usr/local/nginx/,目录下的内容分别为:  
conf,主配置文件目录  
html,网站根目录  
logs,日志文件目录  
sbin,主程序目录


二、Nginx编译选项

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1、默认自动编译项                           禁用选项
Core:Nginx核心功能,                        --without-http
Access:基于IP的访问控制                    --without-http_access_module
Auth Basic:HTTP用户认证模块                --without-http_auth_basic_module
Auto Index:自动目录索引                    --without-http_autoindex_module
Browser:描述用户代理                        --without-http_charset_module
Charset:重新编码网页                        --without-http_charset_module
Empty GIF:内存中存放一个图片                --without-http_empty_gif_module
FastCGI:FastCGI支持                        --without-http_fastcgi_module
Geo:支持IP变量设置                            --without-http_geo_module
Gzip:Gzip压缩                                --without-http_gzip_module
Limit Requests:限制客户端连接频率            --without-http_limit_req_module
Limit Conn:挥发的并发连接                    --without-http_limit_conn_module
Map:设置变量                                --without-http_map_module
Memcached:Memcache支持                        --without-http_memcached_module
Referer:基于Referer头部信息过滤            --without-http_referer_module
Rewrite:使用正则表达式重写请求                --without-http_rewrite_module
SCGI:支持SCGI协议                            --without-http_scgi_module
Upstream:负载均衡                            --without-http_upstream_ip_hash_module
Headers:设置http响应的头部信息
Index:首页
Log:自定义日志




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2、内置模块中的附加模块,需要在编译时手动开启    开启选项
Embedded Perl:支持Perl                            --with-http_perl_module
FLV:支持Flash视频                                --with-http_flv_module
GeoIP:通过IP变量实现负载均衡                    --with-http_geoip_module
Google Perftools:支持谷歌的性能优化工具        --with-google_perftools_module
Gzip Precompression:压缩静态文件                --with-http_gzip_static_module
Image Filter:转换图形的过滤器                    --with-http_image_filter_module
MP4:支持MP4                                    --with-http_mp4_module
Real IP:使用Nginx作为后端服务器                --with-http_realip_module
Secure Link:使用密匙保护页面                    --with-http_secure_link_module
SSL:支持HTTPS/SSL                                 --with-http_ssl_module
Stub Status:查看服务器状态                        --with-http_stub_status_module
WebDAV:支持WebDAV                                --with-http_dav_module
------------------------------------------
Core:邮件代理功能                                --with-mail
Core:邮件代理功能                                --without-mail_pop3_module
Core:邮件代理功能                                --without-mail_imap_module
Core:邮件代理功能                                --without-mail_smtp_module
------------------------------------------
SSL:支持SSL/TLS加密邮件协议                    --with-mail_ssl_module




三、Nginx进程管理命令

?
1
2
3
4
/usr/local/nginx/sbin/nginx     #启动主程序
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf     #指定配置文件启动主程序
/usr/local/nginx/sbin/nginx -s stop     #关闭主程序
/usr/local/nginx/sbin/nginx -s reload     #重新加载设置



Nginx会将进程号保存在/usr/local/nginx/logs/nginx.pid文件中,可以使用kill指令发送信号给该进程号平滑重启Nginx  
  
?
1
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`





四、Nginx配置文件解析

Nginx默认配置文件为/usr/local/nginx/conf/nginx.conf,其内容如下

?
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
124
#user  nobody;    #设置用户与组
worker_processes  1;    #启动子进程数,可以通过ps aux | grep nginx查看

#错误日志文件及日志级别notice,info
#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;    #每个进程可以处理的连接数,受系统文件句柄限制,ulimit
}


http {
    include       mime.types;    #mime.types为文件类型定义文件
    default_type  application/octet-stream;    #默认文件类型

    #log_format自定义入职格式,名称为main
    #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;        #创建访问日志,采用main定义的格式

    sendfile        on;        #是否调用sendfile() 进行数据复制,sendfile复制数据是在内核级别完成的,会比一般的read、write更高效
    #tcp_nopush     on;

    #keepalive_timeout  0;        #保持连接的超时时间
    keepalive_timeout  65;

    #gzip  on;        #是否采用压缩功能,将页面压缩后传输更节省流量

    #使用server定义虚拟主机
    server {
        listen       80;        #服务器监听的端口
        server_name  localhost;        #访问域名

        #charset koi8-r;        #编码格式,如果网页编码与此设置不同,则将被自动转码

        #access_log  logs/host.access.log  main;        #设置虚拟主机的访问日志路径及格式

        #对URL进行匹配
        location / {
            root   html;        #root表示网页根路径,使用的是相对路径,html在nginx安装路径下
            index  index.html index.htm;        #定义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
        #访问以php结尾的页面,则自动将该请求转至127.0.0.1:80,通过proxy_pass可以实现代理功能
        #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
        #
        #拒绝所有人访问.ht页面
        #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;        #监听TLS使用的
    #    server_name  localhost;

    #    ssl                  on;   #开启ssl功能
    #    ssl_certificate      cert.pem;        #指定证书文件,相对路径,文件需放在niginx.conf同目录下
    #    ssl_certificate_key  cert.key;        #指定私钥文件,相对路径,文件需放在niginx.conf同目录下

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}



运维网声明 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.iyunv.com/thread-84557-1-1.html 上篇帖子: centos6.5 x86_64下yum安装nginx和php 下篇帖子: nginx与php整合 配置文件 without 第三方 local 软件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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