需要进入 [nginx]的安装目录中的
sbin目录
(也可以配置环境变量,在任何目录都可以执行下面的命令),里面有一个 nginx 脚本文件
1、启动nginx
./nginx
2、关闭nginx
./nginx -s stop
3、重新加载nginx (nginx.conf)
./nginx -s reload
4、查看版本号
./nginx -v
默认在Linux上安装的Nginx,配置文件在安装的nginx目录下的conf目录下,名字叫做nginx.conf
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块
events {
worker_connections 1024;
}
# http块
http {
------------------------------------------------------------------------------# 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块
server {
# server全局块
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location块
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# 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;
#}
}
# 可以配置多个server块
}
就是配置文件从头开始到 events 块之间的内容,
主要设置的是影响nginx服务器整体运行的配置指令
比如 worker_process, 值越大,可以支持的并发处理量也越多,但是还是和服务器的硬件相关
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
上述例子就表示每个 work process 支持的最大连接数为 1024. 这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置
包括 http 全局块,以及多个 server 块
http 全局块配置的指令包括文件引入、 MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
全局 server 块
,以及可以同时包含多个
location 块
。最常见的配置是本虚拟机主机的
监听配置和本虚拟主机的名称或 IP 配置
。
#这一行表示这个server块监听的端口是80,只要有请求访问了80端口,此server块就处理请求
listen 80;
# 表示这个server块代表的虚拟主机的名字
server_name localhost;
主要作用是根据请求地址路径的匹配,匹配成功进行特定的处理
# 表示如果请求路径是/就是用这个location块进行处理
location / {
root html;
index index.html index.htm;
}
真实的服务器地址
服务器端
,客户端不需要任何配置,客户端只需要将请求发送给反向代理服务器即可
,代理服务器将请求分发给真实的服务器,获取数据后将数据转发给你。隐藏了真实服务器,有点像网关。正向代理与反向代理的区别
最根本的区别是代理的对象不同
客户端
,需要为每一个客户端都做一个代理服务器,客户端访问的路径是目标服务器
真实服务器
,客户端不需要做任何的配置,访问的路径是代理服务器
,由代理服务器将请求转发到真实服务器实现效果访问 http://192.168.80.102:80(Nginx 首页), 最终代理到 http://192.168.80.102:8080(Tomcat 首页)
首先启动一台 Tomcat 服务器 (已经安装了 Tomcat)
进入 Tomcat 的安装目录下的bin目录下
,使用./startup.sh
命令,启动 Tomcat
在 Nginx 的配置文件中进行配置
1、新建一个 server 块,在 server 全局块中配置监听 80 端口
2、在 location 块中配置 / 路径请求代理到 tomcat 的地址
下面三个配置的含义就是 ,当访问 Linux 的 http://192.168.80.102:80 这个地址时,由于配置 Nginx 监听的是 80 端口,所以会进入这个 server 块进行处理,然后看你的访问路径,根据 location 块配置的不同路径进入对应的处理,由于配置了 / 请求,所以进入 / 的 location 处理,然后配置了 proxy_pass,所以进行代理到指定的路径。
server {
# 监听端口80 即当访问服务器的端口是80时,进入这个server块处理
listen 80;
# server_name当配置了listen时不起作用
server_name localhost;
# location后面代表访问路径 当是/ 请求时 代理到tomcat的地址
location / {
# 使用 proxy_pass(固定写法)后面跟要代理服务器地址
proxy_pass http://192.168.80.102:8080;
}
}
经过测试,当输入http://192.168.80.102:80时,Nginx给我们代理到了Tomcat,所以显示了Tomcat的页面,即配置成功
应用一访问的是 / 路径,给我们代理到指定的服务器
应用二实现:
启动两个 Tomcat 服务器
由于虚拟机的 ip 是 192.168.80.102,所以保证访问 http://192.168.80.102:8081/edu/test.html 和 http://192.168.80.102:8082/vod/test.html 都可以成功访问
编写 Nginx 配置文件
server {
# 监听9001端口
listen 9001;
# 进行路径匹配,匹配到edu代理到8081
location ~/edu/ {
proxy_pass http://192.168.80.102:8081;
}
# 进行路径匹配,匹配到vod代理到8082
location ~/vod/ {
proxy_pass http://192.168.80.102:8082;
}
}
经过测试,访问成功!!!
客户端通过域名访问服务器时会将域名与被解析的 ip 一同放在请求中。当请求到了 nginx 中时。
nginx会先去匹配ip,如果listen中没有找到对应的ip,就会通过域名进行匹配
,匹配成功以后,再匹配端口。当这三步完成,就会找到对应的 server 的 location 对应的资源。
简单来说就是使用分布式的场景,将原先的一台服务器做成一个集群,然后将请求分发到各个服务器上,但是,如何将请求每次转发到不同的服务器呢,Nginx 就可以做到。原来我们都是直接访问服务器,现在我们可以使用 Nginx 进行反向代理,然后我们访问 Nginx,
由Nginx将我们的请求分发到不同的服务器上,以实现负载均衡
实现:
访问 http://192.168.80.102:80/edu/test.html,Nginx 将请求分配到 8081 和 8082 两台 tomcat 服务器上。
1、开启两台tomcat
分别在 webapps 下的 edu 下编写一个 test.html,文件内容可以不一致,为了明显看到负载均衡的效果
2、配置文件
# 在http块中的全局块中配置
# upstream固定写法 后面的myserver可以自定义
upstream myserver{
server 192.168.80.102:8081;
server 192.168.80.102:8082;
}
# server配置
server {
# 监听80端口
listen 80;
#location块
location / {
# 反向代理到上面的两台服务器 写上自定义的名称
proxy_pass http://myserver;
}
}
访问 http://192.168.80.102:80/edu/test.html 时,可以分发到 8081 和 8082 两台服务器,测试成功
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除
weight 代表权重默认为 1, 权重越高被分配的客户端越多
upstream myserver {
server 192.168.80.102:8081 weight=1 ;
server 192.168.80.102:8082 weight=2 ;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 问题
#配置负载均衡的服务器和端口
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
ip_hash;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
#配置负载均衡的服务器和端口
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
fair;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}
我们可以将静态资源直接部署在专门的服务器上,也可以直接放在反向代理服务器上(Nginx)所在在的服务器上
然后动态资源还是部署在服务器上,如 tomcat。准备工作:在 Linux 的根目录下 / 的 staticResource 目录下创建两个文件夹,分别是 www 和 image,在 www 目录下创建一个 okc.html, 在 image 目录下放一张 ttt.jpg
实现效果,访问 http://192.168.80.102:80/www/okc.html 和 http://192.168.80.102:80/image/ttt.img 时可以成功访问资源
配置
server {
listen 80;
# 当访问路径带了www时,进入这个location处理,去/staticResource目录下对应的www目录 去找okc.html
# 即最终实现访问到这个路径
# http://192.168.80.102:80/staticResource/www/okc.html
location /www/{
root /staticResource/;
index index.html index.htm;
}
# 跟上面一样
location /image/{
root /staticResource/;
}
}
经过测试,成功访问
示例如下:
alias
location ^~ /sta/ {
alias /usr/local/nginx/html/static/;
}
root
location ^~ /tea/ {
root /usr/local/nginx/html/;
}
tea
/tea1.html 文件扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有