目錄
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
?