nginx初学指南

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwertyupoiuytr/article/details/78331297

nginx 概述:

nginx 服务由一个 master 进程和多个 worker 进程组成,master 进程主要负责读取和应用配置,以及维护 worker 进程,worker 进程负责实际处理请求。Nginx 基于事件处理机制来高效地将请求分配给 worker 进程,worker 进程的数量可以在配置文件中预先定义好或者基于可用CPU数量自动调整匹配。

 

nginx 配置文件:

  1. 配置文件默认路径为 /usr/local/nginx/conf/nginx.conf,或者 /etc/nginx/nginx.conf,或者 /usr/local/etc/nginx/nginx.conf
  2. 配置文件由指令和指令块(block)组成,单个指令有指令名和参数组成,英文分号(;)结尾。指令块由大括号({})将多个指令包括起来,如果一个指令块中可以包含其他指令块,则称这个指令块为一个上下文(context),例如 eventshttpserverlocation 等。

如果指令没有被包括任何上下文中,则认为其在上下文 main 中。events http 的指令被放在上下文 main 中,server 的指令在上下文 http 中,location 的指令在 server 中。

配置文件中通过 # 来定义注释。

 

nginx 进程启动后,可以通过nginx -s <signal> 参数向 master 进程发送信号,信号有下面几种:

  1. stop:快速停止 nginx 进程。
  2. quit:同样是停止进程,但是会等待全部 worker 进程处理完成当前已经收到的请求。
  3. reload:当 master 进程收到 reload 信号后,会首先检查新的配置文件的语法,如果检查通过,master 进程会启动新的 worker 进程,并发消息给旧的 worker 进程通知其关闭,当 worker 进程收到关闭通知后,会停止接收新的请求,处理完成当前处理中的请求,然后退出。如果配置文件检查没有通过,master 进程会回滚配置并仍然使用原有配置工作。
  4. reopen:重新打开(新的)日志文件,该操作可以用于日志的分割,例如当日志积累到一定大小后,先使用 mv 命令将原日志移动到新的目录下,然后使用 reopen 信号告知 nginx 重新打开新的日志文件用于日志记录。

 

nginx 反向代理和负载均衡的基础配置实验:

nginx服务器我们安装在一台CentOS 7.3上面,后端两台server一台是CentOS 6.9搭建的Apache,一台用Windows Server 2012搭建的IIS

扫描二维码关注公众号,回复: 5362283 查看本文章

 

CentOS6.9搭建Apache

安装:yum installhttpd

启动服务:servicehttpd start

配置开机启动:chkconfighttpd on

找一个测试用的http模板网站上传到httpd默认目录/var/www/html下(在/etc/httpd/conf/httpd.conf中配置),如果需要的话,调整一下iptables。测试站点是否可以访问:

C 0 0 139.219.111.108/index.html BRILLIANT Dashboard UI Elements Charts Tabs & Panels Responsive Tables Forms Multi-Level Dropdown Empty Page Dashboard Welcome John Doe Home Dashboard / Data 44,023 DAILY VISITS Line Chart Profit 32,850 SALES Sales 56,150 COMMENTS Bar Chart Customers 89,645 DAILY PROFITS No. of Visits

 

Windows Server 2012搭建IIS站点:

安装:在添加删除角色和功能(Rolesand Features)中添加Web Server (IIS)

Select server roles Before You Begin Installation Type Server Selection Server Role: Features Web Server Role (IIS) Role Services Confirmation d Roles and Features Wizar Select one or more roles to install on the selected server. Roles Active Directory Rights Management Services Application Server DHCP Server DNS Server Fax Server File And Storage Services (Installed) Hyper v Network Policy and Access Services Print and Document Services Remote Access Remote Desktop Services Volume Activation Services eb Server (IIS] Windows Deploymnent Services Windows Server Update Services DESTINATION SERVER Description Web Server (IIS) prcNides a reliable, manageable, and scalable Web application infrastructure. Cancel

默认安装后,找一个测试用的http模板网站上传到http默认目录C:\inetpub\wwwroot下,如果需要的话,调整一下防火墙规则。测试站点是否可以访问:

0 139.219.65.87 MARBLE HOME BLOG PORTFOLIO ABOUT CONTACT 2016 Blend Free HTMLS All Rights Reserveff More Templates - Collect from ive is How Give We the User New Superpowers plates LIVE PREVIEW - Collect from LEARN MORE*

 

安全起见我们可以把后端这两台serverhttp侦听端口修改为一个高位端口,例如:61234

CentOS 6.5上面修改/etc/httpd/conf/httpd.conf中的Listen端口,Windows IIS上面修改bind中的绑定端口。

 

配置好之后,开始搭建nginx

首先添加nginxrepo,添加/etc/yum.repos.d/nginx.repo文件,写入下面的内容:

[nginx]

name=nginxrepo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

保存退出,使用yuminstall nginx安装。

安装完成后启动服务:systemctlstart nginx

设置开机启动:systemctlenable nginx

测试nginx安装是否成功:

0 139.219.7048 Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to ngjnx:ocg. Commercial support is available at nginx.com. Thank you for using nginx.

 

接下来就是配置nginx配置文件了,由于nginx模块非常多,所以可以变查变学习nginx各个模块对应的配置,可以参考官网上面的说明:

http://nginx.org/en/docs/

举个例子,要配置http的配置,就可以在上面链接中找到ngx_http_core_module模块,打开对应的链接,里面会有http配置的详细指令和参数说明,如果要了解http下面的upstream模块,可以找到ngx_http_upstream_module的链接查看。

 

接着我们配置一个简单的负载均衡,修改/etc/nginx/nginx.conf文件内容如下:

user  nginx;

worker_processes  1;

 

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

http{

    include /etc/nginx/mime.types;

    default_type application/octet-stream;

 

    upstream backend{

        server 172.16.0.11:61234;

        server 172.16.0.6:61234;

    }

 

    server{

        listen 80;

        server_name localhost;

        location / {

            proxy_pass http://backend;

        }

    }

}

上面配置里面172.16.0.11172.16.0.6分别是前面我们做的IISApache的机器的内网地址,配置完成后使用nginx -s reload

 

尝试访问一下nginx,发现提示502 Bad Gateway

C O G) 139.219.66.126 502 Bad Gateway nginxfl.12_l

 

首先使用paping测试一下nginx到两台后端web服务器是否可达:

[root@DanCentOS73 ./paping -c 4 172.16.€.6 paping v1.5.5 - Copyright (c) 2611 Mike Lovell -p 61234 Connecting to 172.16.e.6 Connected to 172.16.e.6: Connected to 172.16.e.6: Connected to 172.16.e.6: Connected to 172.16.e.6: Connection statistics: on TCP 61234: t im e=e. 87ms t im e=e. ggms t im e=B. 86ms t ime=e. glms p rotocol= protocol= protocol= p rotocol= Failed TCP port-61234 TCP port-61234 TCP port-61234 TCP port-61234 e (8.88%) Attanpted = 4, Connected = 4, Approximate connection times: Minimum = 8.86ms, [root@DanCentOS73 paping v1.5.5 - Copyright connecting to 172.16.e.11 Connected to 172.16.8.11: Connected to 172.16.8.11: Connected to 172.16.8.11: Connected to 172.16.8.11: Connection statistics: Maximum = e.ggms, Average e. glms ./paping -c 4 172.16. (c) 2611 Mike Lovell on TCP 61234: t im e=1.8gms t im e=l. g2ms t im e=l. BBms t ime=e. 7gms 4, p rotocol= protocol= protocol= p rotocol= Failed €.11 -p 61234 TCP port-61234 TCP port-61234 TCP port-61234 TCP port-61234 e (8.88%) Attanpted = 4, Connected Approximate connection times: Minimum = B.7gms, Maximum 1.g2ms, Average = 1.4Bms

 

再测试一下web页面是否能够返回内容:

[root@Dancent0S73 curl http://172.16.€.6:61234 *DOCTYPE html>  Free Bootstrap Admin c! Bootstrap Styles--> clink .css" /> - FontAwesome Styles--> clink /> c! Morris Chart Styles--> clink c! Custom Styles--> clink -styles.css" /> c! Google Fonts--> clink href= •https://fonts.googleapis.com/css?family=Open+Sans• clink -Chart/cssCharts .css type= •text/css ' <bodp <div •wrapper <nav navbar-default top -navbar" <div role= "navigation

 

测试发现都没问题并且nginx的配置也正常,再查看一下nginxerrorlog

[root@DanCentOS73 tail -n 26 /var/log/nginx/error.log 2€17/1€/24 [crit] 3781#3781: *139 connect() to 172.16.€.6:61234 failed (13: Pemission denied) while connecting to upstream, client: 1@6.12€.78.19€, server: localhost, request: "GET / HTTP/I.I", upstream: "htt host: "139.219.66.126" 2€17/1€/24 €7:52 [warn] 3781#3781: *139 upstream server tanporarily disabled while connecting to upstream, client: 1@6.12€.78.19€, server: localhost, request: "GET / HTTP/1.r•, upstream: 'http://172.16.€.6:61234/", host: "139.219.66.126" 2€17/1€/24 [crit] 3781#3781: *139 connect() to 172.16.€.11:61234 failed (13: Pemission denied) whil e connecting to upstream, client: 1@6.12€.78.19€, server: localhost, request: "GET / HTTP/I.I", upstream: •ht host: "139.219.66.126" 2€17/1€/24 [warn] 3781#3781: *139 upstream client: 1@6.12€.78.19€, server: localhost, request: host: "139.219.66.126" 2€17/1€/24 [error] 3781#3781: *139 no live 8.196, server: localhost, request: "GET / HTTP/I.I", 2€17/1€/24 [error] 3781#3781: *139 no live 8.196, server: localhost, request: "GET / HTTP/I.I", 2€17/1€/24 [error] 3781#3781: *139 no live localhost, request: "GET / HTTP/I.I", 8.196, server: server tanporarily disabled while connecting to upstream, "GET / HTTP/1.r•, upstream: upst reams upst ream : upst reams upst ream : upstreams upst ream : while connecting to "http://backend/ • , while connecting to "http://backend/ • , while connecting to "http://backend/", 'http://172.16.€.11:61234/", upstream, client: 1@6.12€.7 host: "139.219.66.126" upstream, client: 1@6.12€.7 host: "139.219.66.126" upstream, client: 1@6.12€.7 host: "139.219.66.126"

 

可以看到在连接后端两台web服务器的时候报Permission denied,于是使用setenforce0selinux关闭,再次访问发现正常了:

C 0 0 139.219.66.126 MARBLE HOME BLOG PORTFOLIO ABOUT CONTACT 2016 Blend Free HTMLS All Rights Reserved We are Happy to Create Newest Modern Websites LIVE PREVIEW LEARN MORE*

多测试几次看一下负载均衡是否运行正常:

C 0 0 139 219.66.126 BRILLIANT Dashboard UI Elements Charts Tabs & Panels Responsive Tables Forms Multi-Level Dropdown Empty Page Dashboard Welcome John Doe Home Dashboard / Data 44,023 DAILY VISITS Line Chart Profit Line Chart 200 32,850 SALES Sales 56,150 COMMENTS Bar Chart Customers Bar Chart Example 89,645 DAILY PROFITS No. of Visits 46%

 

测试没问题,最后把selinux配置文件修改下,大功告成:

[root@DanCentOS73 nginx]# vim /etc/selinux/config # This file controls the state of SELinux on the systan. # SELIWX= can take one of these three values: enforcing - SELinux security policy is enforced. permissive - SELinux prints warnings instead of enforcing. disabled - No SELinux policy is loaded. SELIwx=disab1ed # SELIWXTYPE= can take one of three two values: targeted - Targeted processes are protected, minimum - Modification of targeted policy. Only selected processes are protected. mls - Multi Level Security protection. SELIWXTYPE=ta rgeted

 

当然,nginx能够实现的功能以及后续的优化还有很多,具体就需要各位看官实践出真知了~~

猜你喜欢

转载自blog.csdn.net/qwertyupoiuytr/article/details/78331297