许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  Nginx基础知识详解

Nginx基础知识详解

阅读数 10
点赞 0
article_banner

一、nginx的安装

linux上的软件我大部分都放在/opt/modules目录下面,这里我把Nginx安装在/usr/ local  /nginx下面.

1.首先安装c语言环境

yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel

2.安装nginx

首先把nginx安装包上传到/opt/modules下面,通过 tar -zxvf 压缩包解压,然后进入解压后的文件,进行编译和安装。对于已经安装过Nginx,然后需要添加其他模块,在configure后面添加就行了,然后执行make就行了,把objs下的nginx文件替换下就能添加新模块了.

./configure --prefix=/usr/local/nginx
make
make install

3.把nginx安装成系统服务

vi /usr/lib/systemd/system/nginx.service
服务脚本内容
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

重新加载系统服务

systemctl daemon-reload

启动服务

systemctl start nginx.service

开机自启

systemctl enable nginx.service

二、nginx基础知识

1.servername匹配规则

完整匹配

   我们可以在同一servername种匹配多个域名

server_name  hsh1.com hsh2.com

通配符匹配

sever_name  *.hsh.com

通配符结束匹配

server_name hsh.*

正则匹配

server_name ~^[0-9]+\.hsh\.com$;

2.反向代理配置

在location中添加proxy_pass

location / {
proxy_pass http://hshd/;
}

server 上面添加upstream

upstream hshd {
server 192.168.44.102:80;
server 192.168.43.103:80;
}

3.负载均衡策略

轮询

   默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求.
weight(权重)

   指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况

upstream httpd {
server 127.0.0.1:8050 weight=10;
server 127.0.0.1:8060 weight=1 backup;
}
  • down:表示当前的server暂时不参与负载
  • weight:默认为1.weight越大,负载的权重就越大。
  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

ip_hash

   根据客户端的 ip地址  转发同一台服务器,可以保持回话。
url_hash

   根据用户访问的url定向转发请求

4.动静分离

1.nginx上配置静态资源

   直接把静态资源放到nginx上,然后添加一个location来配置静态资源的请求

location ~*/(css|img|js) {
        root /usr/local/nginx/static;
        index index.html index.htm;
}

5.alias与root

root的话会加上location后面的路径,而alias不会

location /css {
           alias /usr/local/nginx/static/css;
          index index.html index.htm;
}

6.centos7防火墙开启与关闭

应用服务器防火墙配置

开启防火墙    systemctl start firewalld
重载规则    firewall-cmd --reload
查看已配置规则   firewall-cmd --list-all
指定端口和ip访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.44.101"
port protocol="tcp" port="8080" accept"
移除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source
address="192.168.44.101" port port="8080" protocol="tcp" accept"
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空