轻量级web服务器-Nginx的入门
目錄
Nginx輕量級(jí)web服務(wù)器
前言
一、如何進(jìn)行nginx的安裝
1、首先要檢查nginx是否已經(jīng)安裝
?
2、使用yum命令安裝
?
二、使用nginx進(jìn)行反向代理
1、首先安裝兩個(gè)tomcat,端口分別是8081和8082
2、配置兩個(gè)tomcat
3、修改host文件
4、配置nginx服務(wù)器
總結(jié)
前言
Nginx是一款輕量級(jí)Web服務(wù)器,也是一款反向代理服務(wù)器。使用nginx可以幫助實(shí)現(xiàn)前端動(dòng)靜分離,可以作為郵件代理服務(wù)器,也可以進(jìn)行反向代理加速,支持FastCGI,實(shí)現(xiàn)簡單的負(fù)載均衡和容錯(cuò);同時(shí)nginx的模塊化的結(jié)構(gòu),可以更好的過濾不同的請求,對不同的請求進(jìn)行分發(fā)處理;另外nginx還支持SSL 和 TLS SNI。
Nginx支持熱部署,啟動(dòng)速度特別快,還可以在不間斷服務(wù)的情況下對軟件版本或配置進(jìn)行升級(jí),即使運(yùn)行數(shù)月也無需重新啟動(dòng)。在微服務(wù)的體系之下,Nginx正在被越來越多的項(xiàng)目采用作為網(wǎng)關(guān)來使用,配合Lua做限流、熔斷等控制。對于Nginx的初學(xué)者可能不太容易理解web服務(wù)器究竟能做什么,特別是之前用過Apache服務(wù)器的,以為Nginx可以直接處理php、java,實(shí)際上并不能。對于大多數(shù)使用者來說,Nginx只是一個(gè)靜態(tài)文件服務(wù)器或者h(yuǎn)ttp請求轉(zhuǎn)發(fā)器,它可以把靜態(tài)文件的請求直接返回靜態(tài)文件資源,把動(dòng)態(tài)文件的請求轉(zhuǎn)發(fā)給后臺(tái)的處理程序,例如php-fpm、apache、tomcat、jetty等,這些后臺(tái)服務(wù),即使沒有nginx的情況下也是可以直接訪問的(有些時(shí)候這些服務(wù)器是放在防火墻的面,不是直接對外暴露,通過nginx做了轉(zhuǎn)換)。
一、如何進(jìn)行nginx的安裝
僅提供linux系統(tǒng)安裝方式(使用yum命令安裝)
1、首先要檢查nginx是否已經(jīng)安裝
nginx -V
2、使用yum命令安裝
由于nginx不在centos的官方軟件源下面,所以不可以直接使用yum。要先執(zhí)行
yum install epel-release
輸入“y”,進(jìn)行安裝后,再執(zhí)行
yum install nginx
一直輸入“y”,直到安裝成功。
然后查看版本。nginx -v
使用ip訪問。http:ip/,如下圖顯示,說明安裝成功。
二、使用nginx進(jìn)行反向代理
1、首先安裝兩個(gè)tomcat,端口分別是8081和8082
下載tomcat方式略過。建議下載tomcat8.x以及以上版本。
下載后進(jìn)行解壓縮。
tar -zxvf?apache-tomcat-8.5.63.tar.gz
2、配置兩個(gè)tomcat
解壓后,把a(bǔ)pache-tomcat-8.5.63復(fù)制成兩份,一份是tomcat8081,一份是tomcat8082,過程如下
cp -r apache-tomcat-8.5.63 tomcat-8081
cp -r apache-tomcat-8.5.63 tomcat-8082
然后對兩個(gè)tomcat的端口進(jìn)行修改
<!-- 修改一--> <Server port="8006" shutdown="SHUTDOWN"> <!-- 修改二--> <Connector port="8081" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" /> <!-- 修改一--> <Server port="8007" shutdown="SHUTDOWN"> <!-- 修改二--> <Connector port="8082" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />然后修改兩個(gè)tomcat里面的webapps文件夾里面的ROOT文件夾里面的index.jsp。修改為下面的內(nèi)容
tomcat1修改為:
<%-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --%> <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <% java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy"); request.setAttribute("year", sdf.format(new java.util.Date())); request.setAttribute("tomcatUrl", "https://tomcat.apache.org/"); request.setAttribute("tomcatDocUrl", "/docs/"); request.setAttribute("tomcatExamplesUrl", "/examples/"); %> <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" /><title><%=request.getServletContext().getServerInfo() %></title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head><body><h1>tomcat8081---index.jsp<h1></body> </html>?tomcat2修改為:
<%-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --%> <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <% java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy"); request.setAttribute("year", sdf.format(new java.util.Date())); request.setAttribute("tomcatUrl", "https://tomcat.apache.org/"); request.setAttribute("tomcatDocUrl", "/docs/"); request.setAttribute("tomcatExamplesUrl", "/examples/"); %> <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" /><title><%=request.getServletContext().getServerInfo() %></title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head><body><h1>tomcat8082---index.jsp<h1></body> </html>然后分別啟動(dòng)兩個(gè)tomcat。通過http:ip:8081/、http:ip:8082/訪問
到此為止tomcat配置已經(jīng)搞定
3、修改host文件
4、配置nginx服務(wù)器
此時(shí)當(dāng)訪問www.nginxtest1.com的時(shí)候,就會(huì)訪問host文件,然后就會(huì)去找上面host文件www.sina.com指向ip所對應(yīng)的的linux服務(wù)器,然后www.nginxtest1.com 默認(rèn)的端口就是80,所以訪問www.nginxtest1.com 的時(shí)候,就會(huì)找到下面的upstream tomcat1,然后下面的upstream tomcat1就會(huì)去找server 47.104.84.32:8081,就會(huì)找到8081端口的tomcat服務(wù)器,然后因?yàn)?span style="color:#ff0000;">upstream tomcat1的默認(rèn)訪問頁是index.jsp,所以就會(huì)訪問8081端口的tomcat服務(wù)器的index.jsp頁面(也就是http://47.104.84.32:8081/index.jsp)
此時(shí)當(dāng)訪問www.nginxtest2.com的時(shí)候,就會(huì)訪問host文件,然后就會(huì)去找上面host文件www.nginxtest2.com指向ip對應(yīng)的linux服務(wù)器,然后www.nginxtest2.com默認(rèn)的端口就是80,所以訪問www.nginxtest2.com 的時(shí)候,就會(huì)找到下面的upstream tomcat2,然后下面的upstream tomcat2就會(huì)去找server?47.104.84.32:8082,就會(huì)找到8082端口的tomcat服務(wù)器,然后因?yàn)?span style="color:#ff0000;">upstream tomcat2的默認(rèn)訪問頁是index.jsp,所以就會(huì)訪問8082端口的tomcat服務(wù)器的index.jsp頁面(也就是http://47.104.84.32:8082/index.jsp)
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/user root; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;events {worker_connections 1024; }http {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 /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;upstream tomcat1 {server 47.104.84.32:8081;}#配置www.houhu.com:80對應(yīng)的服務(wù)器監(jiān)聽端口upstream tomcat2 {server 47.104.84.32:8082;}server {listen 80;server_name www.nginxtest1.com;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://tomcat1;#配置默認(rèn)訪問頁,這里就會(huì)訪問到tomcat2里面的那個(gè)index.jsp文件里面index index.jsp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}server {listen 80;server_name www.nginxtest2.com;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://tomcat2;#配置默認(rèn)訪問頁,這里就會(huì)訪問到tomcat2里面的那個(gè)index.jsp文件里面index index.jsp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}} }此時(shí)訪問www.nginxtest1.com到的就是tomcat8081對應(yīng)的tomcat服務(wù)器
訪問www.nginxtest2.com到的就是tomcat8082對應(yīng)的tomcat服務(wù)器
總結(jié)
以上就是此次給大家分享,有關(guān)nginx進(jìn)行反向代理的配置方法。有什么不懂得歡迎在下方留言。后續(xù)會(huì)繼續(xù)更新nginx相關(guān)的其他配置,比如說動(dòng)靜分離、負(fù)載均衡。所以請繼續(xù)關(guān)注我
總結(jié)
以上是生活随笔為你收集整理的轻量级web服务器-Nginx的入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年欢聚时代实习生招聘面经
- 下一篇: Nginx通过max_fails和fai