轻量级web服务器-Nginx的入门
目錄
Nginx輕量級web服務器
前言
一、如何進行nginx的安裝
1、首先要檢查nginx是否已經安裝
?
2、使用yum命令安裝
?
二、使用nginx進行反向代理
1、首先安裝兩個tomcat,端口分別是8081和8082
2、配置兩個tomcat
3、修改host文件
4、配置nginx服務器
總結
前言
Nginx是一款輕量級Web服務器,也是一款反向代理服務器。使用nginx可以幫助實現前端動靜分離,可以作為郵件代理服務器,也可以進行反向代理加速,支持FastCGI,實現簡單的負載均衡和容錯;同時nginx的模塊化的結構,可以更好的過濾不同的請求,對不同的請求進行分發處理;另外nginx還支持SSL 和 TLS SNI。
Nginx支持熱部署,啟動速度特別快,還可以在不間斷服務的情況下對軟件版本或配置進行升級,即使運行數月也無需重新啟動。在微服務的體系之下,Nginx正在被越來越多的項目采用作為網關來使用,配合Lua做限流、熔斷等控制。對于Nginx的初學者可能不太容易理解web服務器究竟能做什么,特別是之前用過Apache服務器的,以為Nginx可以直接處理php、java,實際上并不能。對于大多數使用者來說,Nginx只是一個靜態文件服務器或者http請求轉發器,它可以把靜態文件的請求直接返回靜態文件資源,把動態文件的請求轉發給后臺的處理程序,例如php-fpm、apache、tomcat、jetty等,這些后臺服務,即使沒有nginx的情況下也是可以直接訪問的(有些時候這些服務器是放在防火墻的面,不是直接對外暴露,通過nginx做了轉換)。
一、如何進行nginx的安裝
僅提供linux系統安裝方式(使用yum命令安裝)
1、首先要檢查nginx是否已經安裝
nginx -V
2、使用yum命令安裝
由于nginx不在centos的官方軟件源下面,所以不可以直接使用yum。要先執行
yum install epel-release
輸入“y”,進行安裝后,再執行
yum install nginx
一直輸入“y”,直到安裝成功。
然后查看版本。nginx -v
使用ip訪問。http:ip/,如下圖顯示,說明安裝成功。
二、使用nginx進行反向代理
1、首先安裝兩個tomcat,端口分別是8081和8082
下載tomcat方式略過。建議下載tomcat8.x以及以上版本。
下載后進行解壓縮。
tar -zxvf?apache-tomcat-8.5.63.tar.gz
2、配置兩個tomcat
解壓后,把apache-tomcat-8.5.63復制成兩份,一份是tomcat8081,一份是tomcat8082,過程如下
cp -r apache-tomcat-8.5.63 tomcat-8081
cp -r apache-tomcat-8.5.63 tomcat-8082
然后對兩個tomcat的端口進行修改
<!-- 修改一--> <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" />然后修改兩個tomcat里面的webapps文件夾里面的ROOT文件夾里面的index.jsp。修改為下面的內容
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>然后分別啟動兩個tomcat。通過http:ip:8081/、http:ip:8082/訪問
到此為止tomcat配置已經搞定
3、修改host文件
4、配置nginx服務器
此時當訪問www.nginxtest1.com的時候,就會訪問host文件,然后就會去找上面host文件www.sina.com指向ip所對應的的linux服務器,然后www.nginxtest1.com 默認的端口就是80,所以訪問www.nginxtest1.com 的時候,就會找到下面的upstream tomcat1,然后下面的upstream tomcat1就會去找server 47.104.84.32:8081,就會找到8081端口的tomcat服務器,然后因為upstream tomcat1的默認訪問頁是index.jsp,所以就會訪問8081端口的tomcat服務器的index.jsp頁面(也就是http://47.104.84.32:8081/index.jsp)
此時當訪問www.nginxtest2.com的時候,就會訪問host文件,然后就會去找上面host文件www.nginxtest2.com指向ip對應的linux服務器,然后www.nginxtest2.com默認的端口就是80,所以訪問www.nginxtest2.com 的時候,就會找到下面的upstream tomcat2,然后下面的upstream tomcat2就會去找server?47.104.84.32:8082,就會找到8082端口的tomcat服務器,然后因為upstream tomcat2的默認訪問頁是index.jsp,所以就會訪問8082端口的tomcat服務器的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對應的服務器監聽端口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;#配置默認訪問頁,這里就會訪問到tomcat2里面的那個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;#配置默認訪問頁,這里就會訪問到tomcat2里面的那個index.jsp文件里面index index.jsp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}} }此時訪問www.nginxtest1.com到的就是tomcat8081對應的tomcat服務器
訪問www.nginxtest2.com到的就是tomcat8082對應的tomcat服務器
總結
以上就是此次給大家分享,有關nginx進行反向代理的配置方法。有什么不懂得歡迎在下方留言。后續會繼續更新nginx相關的其他配置,比如說動靜分離、負載均衡。所以請繼續關注我
總結
以上是生活随笔為你收集整理的轻量级web服务器-Nginx的入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年欢聚时代实习生招聘面经
- 下一篇: 7、门禁控制系统接线原理图,如何安装布线