您的位置:首页 > 运维架构 > Nginx

nginx 如何处理请求系列1-Nginx安装

2018-03-23 09:20 399 查看
一些准备工作,常见问题

简介

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,其将源代码以类BSD许可证的形式发布。现在Nginx已经成为一家拥有各种产品的公司,包括Nginx Plus、Nginx Ctroller、 Nginx Unit、 Nginx Amplify、 Nginx WAF.但是Nginx仍然是开源。整个系列都会依托开源的版本,简称Nginx

安装

两类安装,官方编译好的安装包直接通过源安装即可,二是通过源码包编译安装。
编译好的安装包已经加入依赖关系解决,不用用户自己处理依赖不存在对的情况。源码包安装提供更高的定制性,可最大化性能或功能。

官方编译好的安装包
这里我们使用centos6.x系统

[root@slave2 ~]# vim /etc/yum.repos.d/nginx.repo
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

[root@slave2 ~]# yum  install  nginx -y
Installed:
nginx.x86_64 0:1.12.2-1.el6.ngx
Complete!

[root@slave2 ~]# nginx
nginx        nginx-debug

[root@slave2 ~]# which nginx
/usr/sbin/nginx
[root@slave2 ~]# which nginx-debug
/usr/sbin/nginx-debug
[root@slave2 ~]#  /usr/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@slave2 ~]#  /usr/sbin/nginx-debug -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-debug

nginx安装后加入nginx和nginx-debug命令,nginx-debug比nginx编译参数多了--with-debug开启了debug日志,这在调试nginx时十分有用,但开启后会严重限制nginx的并发数。

编译安装

[root@slave2 ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz [root@slave2 ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@slave2 ~]# cd nginx-1.12.2
[root@slave2 nginx-1.12.2]# ./configure --prefix=/opt/nginx-1.12.2 --with-debug
[root@slave2 nginx-1.12.2]# make -j2
[root@slave2 nginx-1.12.2]# make install

源码包需要自己解决依赖关系~~

管理nginx

安装包

[root@slave2 ~]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@slave2 ~]# /etc/init.d/nginx reload
Starting nginx:                                            [  OK  ]
[root@slave2 ~]# /etc/init.d/nginx stop
Stopping nginx:                                            [  OK  ]

debug版本

[root@slave2 ~]# /etc/init.d/nginx-debug start
Starting nginx:                                            [  OK  ]
[root@slave2 ~]# /etc/init.d/nginx-debug reload
Starting nginx:                                            [  OK  ]
[root@slave2 ~]# /etc/init.d/nginx-debug stop
Stopping nginx:                                            [  OK  ]

源码包

[root@slave2 ~]# /opt/nginx-1.12.2/sbin/nginx
[root@slave2 ~]# /opt/nginx-1.12.2/sbin/nginx -s reload
[root@slave2 ~]# /opt/nginx-1.12.2/sbin/nginx -s stop
[root@slave2 ~]# /opt/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.12.2/conf/nginx.conf test is successful

测试

[root@slave2 ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

默认nginx监听在localhost上,通过curl访问成功。
nginx已经安装成功。如果提示如下,请检查防火墙是否开放80端口访问。

[root@slave2 ~]# curl localhost
curl: (7) couldn't connect to host
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息