nginx

配置文件的结构

nginx由配置文件中指定的指令控制的模块组成。指令分为简单指令和块指令。一个简单的指令由空格分隔的名称和参数组成,并以分号结尾。块指令具有与简单指令相同的结构,但不是以分号结尾,而是以大括号和包围的一组符加指令结束。如果块指令在大括号内部还有其他指令,则称为上下文(例如:events,http,server和location)。
配置文件中放置在任何上下文之外的伪指令被认为是主上下文。events和http指令驻流在主上下文中,server在http中,而location在http块中。

提供静态内容服务(静态网站)

一个重要的Web服务器任务是提供文件(如图像或静态HTML页面)。这里我们来学习如何实现一个示例,根据请求,文件将从不同的本地目录提供:/data/www(可能包含HTML文件)和/ data/images(包含图像)。这将需要编辑配置文件,并使用两个位置块在http块内设置服务器块。
首先,创建/data/www目录,并将一个包含任何文本内容的index.html文件放入其中,并创建/data/images目录并在其中放置一些图像。创建两个目录

mkdir -p /data/{www,images}

分别在上面创建的两个目录中放入两个文件:/data/www/index.html 和 /data/images/logo.png,/data/www/index.html文件的内容就一行,如下

<h1> /data/www/index.html </h1>

接下来,打开配置文件(/usr/local/nginx/conf/nginx.conf)。 默认的配置文件已经包含了服务器块的几个示例,大部分是注释掉的。 现在注释掉所有这样的块,并启动一个新的服务器块:

http {
    server {
    }
}

通常,配置文件可以包括服务器监听的端口和服务器名称区分的几个server块。当nginx决定哪个服务器处理请求后,它会根据服务器块内部定义的location指令的参数测试请求头中指定的URI。
将以下location块添加到服务器(server)块:

http {
    server {
        location / {
            root /data/www;
        }
    }
}

该location块指定与请求中的URI相比较的“/”前缀。 对于匹配请求,URI将被添加到root指令中指定的路径(即/data/www),以形成本地文件系统上所请求文件的路径。 如果有几个匹配的location块,nginx将选择具有最长前缀来匹配location块。 上面的location块提供最短的前缀长度为1,因此只有当所有其他location块不能提供匹配时,才会使用该块。
接下来,添加第二个location块:

http {
    server {
        location / {
            root /data/www;
        }
        location /images/ {
            root /data;
        }
    }
}

它将是以/images/(位置/也匹配这样的请求,但具有较短前缀,也就是“/images/”比“/”长)的请求来匹配。
server块的最终配置应如下所示:

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

这已经是一个在标准端口80上侦听并且可以在本地机器上访问的服务器( http://localhost/ )的工作配置。 响应以/images/开头的URI的请求,服务器将从/data/images目录发送文件。 例如,响应http://localhost/images/logo.png请求,nginx将发送服务上的/data/images/logo.png文件。 如果文件不存在,nginx将发送一个指示404错误的响应。 不以/images/开头的URI的请求将映射到/data/www目录。 例如,响应http://localhost/about/example.html请求时,nginx将发送/data/www/about/example.html文件。
要应用新配置,如果尚未启动nginx或者通过执行以下命令将重载信号发送到nginx的主进程:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
如果错误或异常导致无法正常工作,可以尝试查看目录/usr/local/nginx/logs或/var/log/nginx中的access.log和error.log文件中查找原因。

打开浏览器或使用CURL访问Nginx服务器如下所示
这里写图片描述
完整的nginx.conf文件配置内容如下:

#user  nobody;
worker_processes  1;

#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;
}

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 {
        location / {
            root /data/www;
        }
        location /images/ {
            root /data;
        }
    }
}
设置简单的代理服务器

nginx的一个常见用途是将其设置为代理服务器,这意味着它可作为一个接收请求的服务器,将其传递给代理服务器,从代理服务器中检索响应,并将其发送给客户端。

我们将配置一个基本的代理服务器,它为来自本地目录的文件提供图像请求,并将所有其他请求发送到代理的服务器。 在此示例中,两个服务器将在单个nginx实例上定义。

首先,通过向nginx配置文件添加一个server块来定义代理服务器,其中包含以下内容:

server {
    listen 8080;
    root /data/up1;

    location / {
    }
}

这将是一个监听端口8080的简单服务器(以前,由于使用了标准端口80,所以没有指定listen指令),并将所有请求映射到本地文件系统上的/data/upl目录。 创建此目录并将index.html文件放入其中。 请注意,root指令位于server块上下文中。 当选择用于服务请求的location块不包含自己的root指令时,将使用此root指令。

在上面创建的目录/data/up1中放入一个文件:/data/www/demo.html,文件的内容就一行,如下

</h1>/data/upl/index.html</h1>

接下来,使用上一节中的服务器配置进行修改,使其成为代理服务器配置。 在第一个位置块中,将proxy_pass指令与参数中指定的代理服务器的协议,名称和端口(在本例中为http://localhost:8080):

server {
    location / {
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root /data;
    }
}

我们将修改当前使用/images/prefix将请求映射到/data/images目录下的文件的第二个location块,使其与典型文件扩展名的图像请求相匹配。 修改后的位置块如下所示:

location ~ \.(gif|jpg|png)$ {
    root /data/images;
}

该参数是一个正则表达式,匹配所有以.gif,.jpg或.png结尾的URI。正则表达式之前应该是~字符。 相应的请求将映射到/data/images目录。
当nginx选择一个location块来提供请求时,它首先检查指定前缀的location指令,记住具有最长前缀的location,然后检查正则表达式。 如果与正则表达式匹配,nginx会选择此location,否则选择之前记住的那一个。
代理服务器的最终配置将如下所示:

server {
    location / {
        proxy_pass http://localhost:8080/;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}

此服务器将过滤以.gif,.jpg或.png结尾的请求,并将它们映射到/data/images目录(通过向root指令的参数添加URI),并将所有其他请求传递到上面配置的代理服务器。

要应用新配置,如果尚未启动nginx或者通过执行以下命令将重载信号发送到nginx的主进程:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

首先测试上面配置的 8080 端口的服务,访问服务的8080端口,得到以下结果:
这里写图片描述
再次访问 80 端口(这里只是一个代理,它会把请求转发给8080的服务,由8080端口这这个服务处理并返回结果到客户端),得到以下结果 -
这里写图片描述
完整的配置nginx.conf文件内容如下 -

#user  nobody;
worker_processes  1;

#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;
}

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 8080;
        root /data/up1;

        location / {
        }
    }

    ## 代理配置,数据转发
    server {
        location / {
            proxy_pass http://localhost:8080/;
        }

        location ~ \.(gif|jpg|png)$ {
            root /data/images;
        }
    }
}
设置FastCGI代理

nginx可用于将请求路由到运行使用各种框架和PHP等编程语言构建的应用程序的FastCGI服务器。
使用FastCGI服务器的最基本nginx配置包括使用fastcgi_pass指令(而不是proxy_pass指令),以及fastcgi_param指令来设置传递给FastCGI服务器的参数。 假设FastCGI服务器可以在localhost:9000上访问。 以上一节的代理配置为基础,用fastcgi_pass指令替换proxy_pass指令,并将参数更改为localhost:9000。 在PHP中,SCRIPT_FILENAME参数用于确定脚本名称,QUERY_STRING参数用于传递请求参数。 最终的配置将是:

server {
    location / {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
 }

这将设置一个服务器,将除静态图像请求之外的所有请求路由到通过FastCGI协议在localhost:9000上运行的代理服务器。
有关Nginx+PHP+FastCGI服务器,将在单独的一篇文章:LANM安装和配置中讲解.
参考博客:https://www.yiibai.com/nginx/beginners_guide.html

修改配置nginx /usr/local/nginx/conf/nginx.conf

[root@server1 conf]# vim nginx.conf

  2 user  nginx;
  ...
 12 events {
 13     worker_connections  65535;  #最大连接到nginx的clients数目
 14 }

[root@server1 conf]# useradd -u 800 nginx
[root@server1 conf]# nginx -s reload
[root@server1 conf]# su - nginx
[nginx@server1 ~]$ ulimit -a
...
open files                      (-n) 1024
...

[root@server1 conf]# vim /etc/security/limits.conf 
nginx           -       nofile          65535

[root@server1 conf]# su - nginx
[nginx@server1 ~]$ ulimit -a
...
open files                      (-n) 65535
...

证书认证https:

[root@server1 conf]# vim nginx.conf
 28     tcp_nopush     on;
            ...
 33     gzip  on;
            ...
 98     server {
 99         listen       443 ssl;
100         server_name  localhost;
101 
102         ssl_certificate      cert.pem;
103         ssl_certificate_key  cert.pem;
104 
105         ssl_session_cache    shared:SSL:1m;
106         ssl_session_timeout  5m;
107 
108         ssl_ciphers  HIGH:!aNULL:!MD5;
109         ssl_prefer_server_ciphers  on;
110 
111         location / {
112             root   html;
113             index  index.html index.htm;
114         }
115     }
116 
117 }

[root@server1 conf]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem
        ....
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@localhost
[root@server1 certs]# mv cert.pem /usr/local/nginx/conf/
[root@server1 certs]# nginx -s reload
[root@server1 conf]# vim nginx.conf
 19         upstream westos {    #upstream 负载均衡模块
 20                 server 172.25.254.2:80;
 21                 server 172.25.254.3:8080;
 22         }
                  ....
130         server {
131                 listen 80;
132                 server_name www.westos.org;  ##域名
133 
134                 location / {
135                 #       root /www1;  ##默认发布目录
136                 #       index index.html;   ##发布文件
137                         proxy_pass http://westos;  ##代理头 
138                 }
139         }
140         server {
141                 listen 80;
142                 server_name bbs.westos.org;
143 
144                 location / {
145                         root /www2;
146                         index index.html;
147                 }
148         }
149 }

mkdir /www1
mkdir /www2
vim /www1/index.html
vim /www2/index.html
nginx -t         #检验语言是否正确
nginx -s reload  #刷新(并不是重启)

这里写图片描述

nginx的高可用 
[root@server1 local]# scp -r /usr/local/nginx/ server4:/usr/local/

[root@server4 local]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@server4 local]# useradd -u 800 nginx
[root@server4 local]# nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值