与众不同的生活方式很累人呢,因为找不到借口

    在上一章节中,我们以及了解到正向、反向代理、负载均衡和动静分离的基本概念,安装教程,而在本节中将会了解到在

本文要点:

1.理清概念

2.Linux下Nginx的常用命令

3.Nginx的基本配置文件nginx.conf

4.Nginx 配置实例-反向代理实例 1

1.理清概念

docker创建Nginx容器很简单:共享数据卷,外部访问端口映射

window下运行nginx,只需简单式一直点击“NEXT”安装,然后添加到环境变量即可

在Linux下启动nginx相对比较麻烦,如果你采用其它两种方式,可以跳过本节,本节主要是介绍Linux下的运行命令

无论哪种方式启动Nginx,其核心的配置文件以及接下来的几个配置实例都是通用的

2.Linux下Nginx的常用命令

先关闭防火墙或者是开放目标端口

进入nginx目录中

cd /usr/local/nginx/sbin (前面路径有可能不同,主要是nginx内的nginx/sbin路径)

(1)、查看nginx版本号

./nginx -v

(2)、启动nginx

./nginx

(3)、停止nginx

./nginx -s stop

(4)、重新加载nginx

./nginx -s reload

3.ginx的基本配置文件nginx.conf

3.1、nginx 配置文件位置

cd /usr/local/nginx/conf/nginx.conf

3.2、配置文件中的内容 包含三部分内容

(1)全局块:配置服务器整体运行的配置指令 比如 worker_processes 1;处理并发数的配置

(2)events 块:影响 Nginx 服务器与用户的网络连接 比如 worker_connections 1024; 支持的最大连接数为 1024

(3)http 块 还包含两部分: http 全局块 server 块

3.3 默认的nginx.conf

#user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #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 logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} 404.md # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#}
}

4.Nginx 配置实例-反向代理实例 1

4.1、实现效果

(1)打开浏览器,在浏览器地址栏输入地址 www.123.com,跳转到 liunx 系统 tomcat 主页 面中

4.2、准备工作

(1)在 liunx 系统安装 tomcat,使用默认端口 8080 * tomcat 安装文件放到 liunx 系统中,解压 * 进入 tomcat 的 bin 目录中,./startup.sh 启动 tomcat 服务器

(2)对外开放访问的端口

firewall-cmd --add-port=8080/tcp --permanent

firewall-cmd --reload

(3)查看已经开放的端口号

firewall-cmd --list-all

(4)在 windows 系统中通过浏览器访问 tomcat 服务器(linux_IP + tomcat_Port)

4.3、访问过程分析

    (1)映射IP

假设域名www.123.com映射的IP为Linux上的nginx的ip地址(比如我的是192.168.17.129:80),nginx也相当于一个分发器,将请求发送至tomcat服务器

访问过程分析
windows端 ---------->Nginx(linux_IP:80)------>tomcat(127.0.0.1:8080)
(假设访问www.123.com)

    (2)在 nginx 进行请求转发的配置(反向代理配置)

server{
listen 80; # 监听端口
server_name www.123.com; # 配置域名 location /{
proxy_pass http://127.0.0.1:8080; # 跳转到127.0.0.1:8080路径
index index.html index.htm index.jsp;
}
}

如上配置,我们监听 80 端口,访问域名为 www.123.com,不加端口号时默认为 80 端口,故 访问该域名时会跳转到 127.0.0.1:8080 路径上。在浏览器端输入 www.123.com后,成功后直接跳转到tomcat主页

05-11 22:59