(如有错漏之处,敬请指正)

nginx是什么?

戳这里了解nginx概念
今年年初因为个需求才开始认识nginx的,对方只提供了一个端口访问权限给我们,无法承担起3、4个应用的通信需求,故引入了nginx。nginx也是软件负载均衡的主流中间键,我之前都是通过f5交换机来实现的负载均衡(原来公司有钱任性哈哈)

测试虚拟机版本为:
CentOS Linux release 7.3.1611 (Core)
nginx版本为:
nginx version: nginx/1.10.1
安装路径为:
/usr/local/nginx

以下用到的nginx的重启操作步骤分两步:
先使用 nginx -t 确认配置信息是否正确,如提示如下信息,则配置无误。

ginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

然后使用 nginx -s reload 重启即可

nginx的配置

nginx安装后主要配置的文件夹为conf,其中最为重要的配置文件是nginx.conf,如下:

user "具体用户";
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{use epoll;worker_connections 65535;
}
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';#charset gb2312;include /usr/loca/nginx/conf.d/*.conf;server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 8m;sendfile on;tcp_nopush on;keepalive_timeout 60;tcp_nodelay on;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;#limit_zone crawler $binary_remote_addr 10m;#下面是server虚拟主机的配置server{listen 80;#监听端口server_name localhost;#域名index index.html index.htm index.php;root /usr/local/nginx/html;#站点目录location ~ .*\.(php|php5)?${#fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)${expires 30d;# access_log off;}location ~ .*\.(js|css)?${expires 15d;# access_log off;}access_log off;}
}

我在conf文件夹外部建了个conf.d的文件夹用于存放我新增的nginx配置。通过在上面nginx.conf中添加 include /usr/loca/nginx/conf.d/*.conf来引入外部的配置文件。

nginx的正向代理配置

目标效果图如下
nginx正向代理与反向代理的配置-编程知识网
由于没有环境,只能通过抠脚的方式模拟
nginx正向代理与反向代理的配置-编程知识网
正向代理的配置/usr/loca/nginx/conf.d/ng.conf文件内容如下:

server {# 配置DNS解析IP地址,比如 Google Public DNS,以及超时时间(5秒)# 监听端口listen 8081;access_log  /usr/local/nginx/access.log;error_log   /usr/local/nginx/error.log;location / {# 配置正向代理参数proxy_pass  http://192.168.79.129:8080/;# 解决如果URL中带"."后Nginx 503错误proxy_set_header Host $http_host;# 配置缓存大小proxy_buffers 256 4k;# 关闭磁盘缓存读写减少I/Oproxy_max_temp_file_size 0;# 代理连接超时时间proxy_connect_timeout 30;# 配置代理服务器HTTP状态缓存时间proxy_cache_valid 200 302 10m;proxy_cache_valid 301 1h;proxy_cache_valid any 1m;}
}

以上配置大致意思就是当我请求 nginx 服务(本机)http://localhost:8081时,请求会转发到 http://192.168.79.129:8080这个地址。日志文件分别生成在
access_log /usr/local/nginx/access.log
error_log /usr/local/nginx/error.log

测试

使用 curl ‘http://localhost:8081’ 命令测试,成功返回tomcat首页的html。
同时查看日志文件access.log可以看到如下信息

127.0.0.1 - - [19/Mar/2019:23:13:53 +0800] "GET / HTTP/1.1" 200 11250 "-" "curl/7.43.0"

nginx的反向代理

目标的效果图如下
nginx正向代理与反向代理的配置-编程知识网
因为同样抠脚的原因我的结构如下
nginx正向代理与反向代理的配置-编程知识网
反向代理的配置/usr/loca/nginx/conf.d/ng2.conf文件内容如下:

upstream apachephp  {server localhost:8080; #引流部分,可配置多个服务地址
}
server {listen 8082;#对外提供8082端口#server_name  www.quancha.cn;#access_log  logs/quancha.access.log  main;#error_log  logs/quancha.error.log;#root   html;#index  index.html index.htm index.php;## send request back to apache ##location / {proxy_pass http://apachephp;}}    

以上配置大致意思就是当我请求 nginx 服务(本机)http://localhost:8082时,请求会转发到多个服务地址里,本样例的服务地址就是 http:// localhost:8080这个地址。

测试

通过使用 curl ‘http://localhost:8082’ 命令测试,成功返回tomcat首页的html
同时查看日志文件access2.log可以看到如下信息

127.0.0.1 - - [19/Mar/2019:23:13:56 +0800] "GET / HTTP/1.1" 200 11250 "-" "curl/7.43.0"

总结

这次验证只是简单的对nginx的正向、反向代理功能进行验证。nginx的负载均衡以及性能的优化待下次进一步的跟进(没有测试环境啊~尬住)。