In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Nginx
Nginx (engine x) is a high-performance HTTP and reverse proxy web server that also provides IMAP/POP3/SMTP services. Nginx was developed by Igor Sesoyev for Russia's second most visited Rambler.ru site (Russian: Рамблер), with the first public version 0.1.0 released on October 4, 2004.
It distributes its source code under a BSD-like license and is known for its stability, rich feature set, sample configuration files, and low system resource consumption. On June 1, 2011, nginx 1.0.4 was released.
Nginx is a lightweight Web server/reverse proxy server and email (IMAP/POP3) proxy server distributed under the BSD-like protocol. It is characterized by less memory and strong concurrency capability. In fact, nginx's concurrency capability performs well in web servers of the same type.
Nginx can be compiled and run on most UnixLinux OSes and is ported to Windows. Nginx 1.4.0 stable version has been released on April 24, 2013, under normal circumstances, for new sites, it is recommended to use the latest stable version as the production version, the existing site upgrade urgency is not high.
Nginx source code uses a 2-clause BSD-like license. Nginx is a powerful, high-performance Web and reverse proxy service with many excellent features: Nginx is a good alternative to Apache in cases of high concurrency: Nginx is one of the software platforms often chosen by owners of web hosting businesses in the United States. To be able to support up to 50,000 concurrent connections, thanks to Nginx for choosing epoll and kqueue as our development model. First, the Nginx environment to prepare an nginx server to provide www.accp.com web pages. 1, Install rpm source rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm2, Install nginx and bindyum install nginx bind -y3, DNS domain name resolution vim /etc/named.conf
vim /etc/named.rfc1912.zones ##Copy Add zone "accp.com" IN { type master; file "accp.com.zone"; allow-update { none; };};
cd /var/namedcp -p named.localhost accp.con.zonevim accp.con.zone
4、修改nginx配置文件vim /etc/nginx/conf.d/default.conf
5、启动服务,关闭防火墙systemctl stop firewalld.service setenforce 0systemctl start namedsystemctl start nginx二、Rewrite 介绍2.1、Rewrite跳转场景URL 看起来更规范、合理;企业会将动态URL地址伪装成静态地址提供服务;网址换域名后,让旧的访问跳转到新的域名上;服务端某些业务调整2.2、Rewrite 实用场景1、Nginx跳转需求的实现方式:使用rewrite进行匹配跳转;使用 if 匹配全局变量后跳转;使用 location 匹配再跳转2、rewrite放在 server { },if { };location{ }段中;3、对域名或参数字符串:使用 if 全局变量匹配;使用 proxy_pass 反向代理2.3、常用的正则表达式元字符
2.4、Rewrite 命令语法:
flag标记说明:
last和break比较:
2.5、location 分类分类:location = patt { } [精准匹配]location patt { } [一般匹配]location ~ patt { } [正则匹配]正则匹配的常用表达式:
2.6、location 优先级相同类型的表达式,字符串长的会优先匹配;按优先级排列:= 类型^~ 类型表达式正则表达式(~ 和 ~*)类型常规字符串匹配类型,按前缀匹配通用匹配(/),如果没有其他匹配,任何请求都会匹配到三、具体场景3.1、场景一:基于域名的跳转实验环境:公司旧域名www.accp.com,因业务需求有变更,需要使用新域名www.newaccp.com代替.需求:不能废除旧域名从旧域名跳转到新域名,且保持其参数不变1、修改nginx的配置文件vim /etc/nginx/conf.d/default.conf//添加一段if ($host = 'www.accp.com') { rewrite ^/(.*)$ http://www.newaccp.com/$1 permanent; }//域名重定向:就是当访问www.wang.com时,将激动跳转到www.new.wang.com域名。//permanent:表示永久的意思。
2、DNS服务提供新域名的解析vim /etc/named.rfc1912.zones//复制之前的accp域名声明段修改
cd /var/namedcp -p accp.com.zone newaccp.com.zone
3、重启服务systemctl stop nginxsystemctl start nginxsystemctl restart named验证:在win10的浏览器中输入新域名www.accp.com
3.2、场景二:基于客户端IP地址访问跳转实验要求:今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司的IP才能访问正常。公司IP地址:192.168.111.146PC客户端:192.168.111.140把上一个实验的nginx配置部分删除,以防影响下面的实验。1、修改nginx的配置文件,重启服务vim /etc/nginx/conf.d/default.conflisten 80;server_name www.accp.coom;#charset koi8-r;access_log /var/log/nginx/www.accp.com-access.log main; #设置是否合法的IP标志set $rewrite true;#判断是否为合法IPif ($remote_addr = "192.168.111.146"){ set $rewrite false;}#非法IP进行判断打上标记if ($rewrite = true){ rewrite (.+) /main.html;}#匹配标记进行跳转站点location = /main.html { root /usr/share/nginx/html;}systemctl stop nginxsystemctl start nginx
2、给 main.html 添加自定义页而内容cd /usr/share/nginx/htmlvim main.html test网站 网站维护中,请稍等~~~systemctl restart nginx 用公司的IP地址访问:
通过客户端IP地址访问:
3.3、场景三:基于旧、新域名跳转并加目录例如:现在访问的是 http://bbs.accp.com ,现在需要将这个域名下面的发帖都跳转到 http://www.accp.com/bbs ,注意保持域名跳转后的参数不变。1、在nginx配置文件中添加以下代码vim /etc/nginx/conf.d/default.conf listen 80; server_name bbs.accp.coom; #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log; #添加一段 location /post { rewrite (.+) http://www.accp.com/bbs$1 permanent; }
2、修改域名,重启服务cd /var/namedvim accp.com.zone #把里面的 www 换成 bbs 不然无法解析。systemctl restart nginxsystemctl restart namedecho "nameserver 192.168.111.145" > /etc/resolv.conf
3、在浏览器上访问 http://bbs.accp.com/post/a.html ,会帮我们自动跳转 http://www.accp.com/bbs/post/a.html , 此时域名跳转后的参数并没有变还是bbs
3.4、场景四:基于参数匹配跳转例如:浏览器访问http://www.accp.com/100-(100|200)-100.html,会自动跳转到 http://www.accp.com 的页面。1、修改nginx的配置文件,添加以下代码listen 80; server_name www.accp.coom; #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; ## 添加一段 if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.*) http://www.accp.com permanent; }
server_name www.accp.com;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.test.com permanent;
}
123456
2、DNS解析www。并重启服务 cd /var/namedvim accp.com.zonesystemctl restart nginxsystemctl restart named
3、在浏览器访问 http://www.accp.com/100-100-100.html,就会帮我们自动跳转到www.accp.com网站
3.5、场景五:基于目录下所有php文件跳转例如,我们访问 http://www.accp.com/upload/1.php,会自动跳转到首页www.accp.com。1、修改nginx的配置文件,添加以下代码vim /etc/nginx/conf.d/default.conf listen 80; server_name www.accp.coom; #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main;## 添加 location ~* /upload/.*\.php$ { rewrite (.+) http://www.accp.com permanent; }
2、重启服务systemctl restart nginx3、在浏览器上访问 http://www.accp.com/upload/1.php ,就会帮我们自动跳转到 www.accp.com网页。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.