Nginx配置

nginx安装在IP为x.x.x.x的服务器上

nginx安装

第一步,查看是否安装编译工具及库文件(openssl)。

命令:rpm -q openssl

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre pcre-devel

openssl  version

第二步,安装nginx。

# 下载Nginx安装包

命令:cd /usr/local/src/

wget http://nginx.org/download/nginx-1.20.1.tar.gz

# 解压Nginx安装包

命令:tar zxvf nginx-1.20.1.tar.gz

# 编译安装Nginx

命令:cd nginx-1.20.1

(--with-http_stub_status_module可以用来启用Nginx的NginxStatus功能,以监控Nginx的运行状态。

可以通过./configure --help选项查看更多模块的情况。)

./configure \

--prefix=/usr/local/nginx \

--sbin-path=/usr/sbin/nginx \

--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/tmp/nginx/client \

--http-proxy-temp-path=/var/tmp/nginx/proxy \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre \

--with-http_v2_module \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_addition_module \

--with-http_sub_module \

--with-http_dav_module \

--with-http_flv_module \

--with-http_mp4_module \

--with-http_gunzip_module \

--with-http_gzip_static_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_stub_status_module \

--with-http_auth_request_module \

--with-mail \

--with-mail_ssl_module \

--with-file-aio \

--with-http_v2_module \

--with-threads \

--with-stream \

--with-stream_ssl_module

make && make install

# 查看nginx版本

命令:/usr/sbin/nginx -v

或    /usr/sbin/nginx -V

若结果显示“nginx version: nginx-1.20.1”,则nginx安装完成。

nginx配置

第一步,创建 Nginx 运行使用的用户nginx。

命令:useradd -s /sbin/nologin -M nginx

( Nginx 服务的默认用户是 nobody ,为了安全更改为 nginx,在配置文件中启用user nginx nginx;)

第二步,配置nginx.conf文件—默认网站。

当配置文件nginx.conf中有且只有⼀个server时,该server就被Nginx认为是默认⽹站,所有发给Nginx服务器80端⼝的数据都会默认给该server。

nginx.conf中的server内容如下:

server {

      listen 80;

      server_name localhost;

      location / {

           root html;

           index index.html index.htm;

      }

      error_page 500 502 503 504 /50x.html;

      location = /50x.html {

           root html;

      }

 }

步,配置nginx.conf文件—location

1)Location语法

Location具体语法:

location [ = | ~ | ~* | ^~ ] URL { ... }

重点看方括号中的 [ = | ~ | ~* | ^~ ],其中 | 分隔的内容表示可能会用到的语法,语法的含义如下:

=  表示精确匹配;

^~ 表示URL以某个字符串开头;

~  表示区分大小写的正则匹配;

~* 表示不区分大小写的正则匹配;

/  表示通用匹配。

2)匹配顺序

Location的定义分为两种:

前缀字符串和正则表达式(具体为前面带 ~* 和 ~ 修饰符的)

当存在多个location的时候,匹配的顺序为:

检查使用前缀字符串的locations,前缀字符串顺序不重要,按照匹配长度来确定(选择最长匹配的),并将结果进行储存;

正则表达式则按照定义文件的顺序,检查正则表达式,匹配到就停止;

当正则表达式匹配不到的时候,使用之前储存的前缀字符串。

在匹配优先级上:

= 修饰符最高,^~ 次之,再者是正则(~和~*),最后是前缀字符串(/)匹配。

步,配置nginx.conf文件—防盗链。

1)root与alias区别与访问路径:

alias:实际访问文件路径不会拼接URL中的路径。

location ^~ /sta/ {

   alias /usr/local/nginx/html/static/;

}

请求:http://test.com/sta/sta1.html

实际访问:/usr/local/nginx/html/static/sta1.html

root:实际访问文件路径会拼接URL中的路径。

location ^~ /tea/ {

   root /usr/local/nginx/html/;

}

请求:http://test.com/tea/tea1.html

实际访问:/usr/local/nginx/html/tea/tea1.html

2)只有授权和允许的用户才能访问此网站。编辑nginx.conf文件,加入下面代码:

location /images/ {    #匹配image路径

        alias /data/images/;

        valid_referers none blocked *.ayitula.com;  #设置条件

        if ($invalid_referer) {   #不满足referer

                rewrite ^/ http://www.ayitula.com/daolian.gif;

                #return 403;

        }

}

步,配置nginx.conf文件—反向代理。

1)Nginx作为代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给Nginx,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在Nginx的硬盘中,再发送给客户机。代用户上网访问是正向代理,代服务器访问是反向代理。

2)编辑nginx.conf文件,加入下面代码:

location / {

    proxy_pass http://192.168.1.2:4000; #代理服务器

}

步,配置nginx.conf文件—负载均衡。

Nginx提供了几种分配方式(策略):

1)轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

2)weight

weight代表权,重默认为 1,权重越高被分配的客户端越多指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

3)ip_hash

每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。

4)fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

步,配置nginx.conf文件—动静分离。

 1)Nginx处理静态页面,Tomcat处理动态页面。动静分离从目前实现角度来讲大致分为两种,一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过 nginx 来分开。

2)通过location指定不同的后缀名实现不同的请求转发。通过expires参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。此种方法非常适合不经常变动的资源。

3)编辑nginx.conf文件,加入下面代码:

location /www {

         root /httpsweb;

         index index.html;

}

location /test {

         root /httpsweb;

         autoindex on;

}

4)检查nginx.conf配置是否正确,然后测试动静分离是否成功。然后删除后端tomcat服务器上的某个静态文件,查看能否访问。若可以访问,则说明nginx直接返回静态资源,不走后端tomcat服务器。

步,配置nginx.conf文件—URL重写。

1)nginx的URL重写功能是依靠ngx_http_rewrite_module这个模块来实现的。有5个nginx常用的rewrite语法:

①set:用来设置变量;

②if:用来判断一些在rewrite语句中无法直接匹配的条件,比如检测文件存在与否,http header,cookie等;

③return:可用来直接设置HTTP返回状态,比如403,404等;

④break:立即停止rewrite检测;

⑤rewrite:设置url重写,其语法使用格式为“rewrite 正则 替换 标志位”。如rewrite ^/(.) http://8.142.214.238:9004/$1 parmanent;

说明:正则^/(.)表示匹配所有的请求,匹配成功后,跳转到后面指定的url地址;$1是取出前面正则表达式分组括号里的内容;parmanent表示301重定向的标记。

2)rewrite的重写功能和apache基本上是一样,有4个默认使用的rewrite标志位:

①break:停止rewrite检测,当含有“break flag”的rewrite语句被执行时,该语句就是rewrite的最终结果;

②last:停止rewrite检测,但是跟break有本质的不同,last的语句不一定是最终结果;

③redirect:返回302临时重定向,一般用于重定向到完整的URL(包含http:部分);

④permanent:返回301永久重定向,一般用于重定向到完整的URL(包含http:部分)。

注意:last和break用于实现url重写,浏览器的地址栏不会发生变化;redirect和permanent也是用于url跳转,浏览器url地址栏发生变化,跳转到新的url地址栏。

3)if语句中的判断条件:

正则表达式匹配:~ 为区分大小写匹配;~* 为不区分大小写匹配;

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配。

4)文件及目录匹配:

-f和!-f用来判断是否存在文件;-d和!-d用来判断是否存在目录;

-e和!-e用来判断是否存在文件或目录;-x和!-x用来判断文件是否可执行。

5)编辑nginx.conf文件,加入下面代码:

location /test.html {

    rewrite ^/(.) http://wwww.taobao.com/ redirect;

}

6)重新启动nginx,若在浏览器中输入localhost/index.html,网页跳转到淘宝的登录界面,则证明URL重写成功。

步,配置nginx.conf文件—SSL。

1)nginx支持SSL功能,需要在编译时指定--with-http_ssl_module选项。nginx的SSL功能就是依靠“ngx_http_ssl_module”模块来完成的。

2)要完成对SSL的支持,就需要生成证书。

3)创建ssl密钥目录

命令:mkdir -p /etc/nginx/ssl_key

4)创建私钥

命令:cd /etc/nginx/ssl_key/

openssl genrsa -idea -out server.key 2048

openssl req -days 3650 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

5)配置https网站

新建/etc/nginx/conf.d/https.conf文件,在文件中写入下面代码:

server {

        listen 443 ssl;

        server_name https.benet.com;

        ssl_certificate ssl_key/server.crt;

        ssl_certificate_key ssl_key/server.key;

        location / {

                root /httpsweb;

                index index.html;

        }

}

然后新建文件夹/usr/local/nginx/httpsweb,并在文件夹中生成index.html。

命令:echo "<h1>https.benet.com</h1>" > /usr/local/nginx/httpsweb/index.html

最后重启nginx服务,在浏览器中使用https://https.benet.com访问测试,出现对应网页页面,则nginx的SSL功能配置成功。

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值