Nginx配置文件解析及功能演示
一、实验环境
- 共三台虚拟机、两台httpd、1台nginx ,防火墙全部关闭状态
- httpd1 192.168.0.243 已成功安装apache,并能成功访问测试页
- httpd 2 192.168.0.244 已成功安装apache,并能成功访问测试页
- nginx 192.168.0.250 源码安装
- nginx 版本nginx 1.19
- 一台宿主机做客户端win10
二、nginx配置文件
nginx安装完毕之后,对于初学者来说,眨眼一看,哇塞。这配置文件这么多内容。有点奔溃。没关系同学们先不要怕。
1、nginx完整版配置文件
#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;
}
#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;
#}
}
# 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;
# }
#}
}
2、nginx配置文件纯净版
其实去掉注释之后,也就这么几行。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
三、反响代理
什么是正向代理?什么是反向代理?
正向代理,架设在客户机与目标主机之间,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。
反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器。
反向代理的应用
现在许多大型web网站都用到反向代理。除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。
反向代理实例
单服务器代理访问
下面我们用nginx 反向代理Apache1 (192.168.0.244)
Nginx配置如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.0.244:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
多域名多服务器访问不同网站
我们可以通过不同的域名,让Nginx跳转到不同的服务器。
开始之前先到/etc/hosts 下把ftpserver.com zabbix.com 都映射到nginx服务器。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name ftpserver.com;
location / {
proxy_pass http://192.168.0.243:80;
}
}
server {
listen 80;
server_name zabbix.com;
location / {
proxy_pass http://192.168.0.244:80;
}
}
}
多域名单台服务器实现多个网站
四、负载均衡
代码:
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myweb{
server 192.168.0.243:80 weight=1;
server 192.168.0.244:80 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myweb;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}