目录
apache
nginx
反向代理配置[root@k8s2 ~]# [root@k8s2 ~]# cat /etc/nginx/conf.d/webserver.confserver { listen 80; server_name www.sxy1.com; location / { root /var/www/html; index index.html; } location /py/{ proxy_pass http://127.0.0.1:9000/; }}
DigitalOcean这家云计算提供了一个在线生成nginx配置的工具
我们主要使用nginx来完成两个方向的功能
1.在网站搭建过程中,将静态资源和动态解耦
2。对后台服务器进行反向代理,并进行负载均衡调节
3。什么时候会用到正向代理,假如说我们公司只有一台电脑能够上网,剩下只能公司内部互相访问。
我们很少会用到正向代理
先阶段,一般Apache很少用于给客户端提供web服务,更多的是linux以及一些管理软件,监控平台会使用Apache,一般网站都是用nginx来提供web服务
在RHEL系的发行版中,apache的web服务器对应的软件包名为 httpd,在debian的发行版中,对应的软件名为apache2
apache
我们可以直接使用软件包管理安装器安装相关服务和依赖
yum install -y httpd ###下载apache
systemctl status httpd ##启动apache
systemctl enable httpd ##开机自启apache
vim /var/www/html/index.html ###编辑apache页面
主配置文件 /etc/httpd/conf/httpd,conf
子配置文件 /etc/httpd/conf.d/
数据目录 /var/www/html/
ss -anplt | grep :80 看看端口号,起来没
发布对不在默认的数据目录,如何发布
1.更改发布目录
[root@server ~]# mkdir /srv/myhtml
[root@server ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/srv/myhtml"
/srv/myhtml">
Options Indexes FollowSymLinks
AllowOverride None
2.通过符号连接(软连接)访问到其他目录
[root@server ~]# ln -s /srv/myhtml/ /var/www/html/
访问
http://192.168.10.100/myhtml/
3.别名: 修改配置文件
[root@server ~]# vim /etc/httpd/conf/httpd.conf
alias /myhtml /srv/myhtml
/srv/myhtml">
Require all granted
Options Indexes
[root@server ~]# systemctl restart httpd
nginx
nginx是一个轻量级的代理服务软件,兼具了web服务器的功能,相对于apache而言,nginx内存消耗和并发能力强,而apache更适合一些动态脚本的承载
一般情况下,我们会使用nginx作为对外服务,对内的管理平台大多数还是会以apache为承载
nginx服务目前主要的一个作用就是用来做代理服务,在代理服务中又分为正向代理和反向代理
正向代理:如果我们使用nginx作为正向代理的时候,我们的客户端在访问目标主机之前,会将请求发送到nginx上面,通过nginx再发送带目标主机上,目标主机会认为是从nginx主机发送过来的请求
正向代理是我们打算访问出去的时候,我们的请求是发往nginx的,然后由nginx代替我们访问目标主机
反向代理;
是我们使用nginx作为反向代理,我们客户端会去访问nginx主机,由nginx主机把我们的访问请求转发到服务端上,等待服务端请求完毕之后在回应我们的客户端
一般来说,面向客户端的时候,我们称为正向代理(多个客户端通过一个nginx向外发出请求)面向服务端的时候,我们成为反向代理(多个服务端通过一个nginx接受请求)
什么时候会用到正向代理,假如说我们公司只有一台电脑能够上网,剩下只能公司内部互相访问。
我们很少会用到正向代理
在主机上安装nginx
yum install nginx
dnf install nginx
惯例处理防火墙和启动问题
rocky
systemctl start nginx
systemctl enable nginx
[root@k8s2 usr]# firewall-cmd --add-service=http
[root@k8s2 usr]# firewall-cmd --add-service=http --permanent
红帽体系中默认的web目录为/usr/share/nginx/html
不同于httpd,默认的nginx这边是不会将目录内的内容以网页列表或者表格的形式展现出来,必须使用具体的文件的url的访问方式才可以访问。
nginx默认的配置文件子啊 /vim/etc/nginx/nginx.conf
在配置文件里讲解
###全局配置
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
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的单独配置
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 4096;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; ##指定附加文件的位置
###具体的每一个单独的http连接的管理(例如虚拟主机,代理管理等)
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;error_page 404 /404.html;
location = /404.html {
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }}
我们尝试添加一个正向代理
[root@k8s2 ~]# cat /etc/nginx/conf.d/proxy.conf
server {
listen 10080;
server_name _;
resolver 10.10.10.102;access_log /tmp/proxy.access.log;
error_log /tmp/proxy.error.log,
location / {
proxy_pass $scheme://$host$request_uri;
proxy_set_header Host $httpd_host}
}
我们主要使用nginx来完成两个方向的功能
1.在网站搭建过程中,将静态资源和动态解耦
2。对后台服务器进行反向代理,并进行负载均衡调节
这里我们来模拟进行搭建一个环境,来进行静态资源和动态资源的解耦,并进行一下反向代理的内容
反向代理配置
[root@k8s2 ~]#
[root@k8s2 ~]# cat /etc/nginx/conf.d/webserver.conf
server {
listen 80;
server_name www.sxy1.com;
location / {
root /var/www/html;
index index.html;
}
location /py/{
proxy_pass http://127.0.0.1:9000/;
}
}
@nginx nginx-1.0.9]# cd /usr/local/nginx-1.0.9
[root@nginx nginx-1.0.9]# ./configure --prefix=/usr/local/nginx
[root@nginx nginx-1.0.9]# make && make install
[root@xen01 ~]# sed -i '/^[ ]*#/d; /^$/d' /usr/local/nginx/conf/nginx.conf
[root@nginx conf]# cat -n /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream htmlservers {
ip_hash;
server 192.168.22.69:80;
server 192.168.22.30:80;
}
upstream phpservers {
ip_hash;
server 192.168.22.11:80;
server 192.168.22.12:80;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
if ($request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@nginx conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx conf]# /usr/local/nginx/sbin/nginx
[root@nginx conf]# netstat -tnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11276/nginx
tcp 0 0 0.0.0.0:8084 0.0.0.0:* LISTEN 5214/stunnel