最近因为web项目多点部署,需要用到反向代理,所以选择了现在和apache同样火热的nginx实践了一下,非常庞大的一个工具,还有很多功能等待使用和研究,以下是初步使用的记录过程:
起因:项目最开始使用的是DNS做负载,但是有一个缓存时间的问题,使用阿里云服务器,最低也有10分钟,无法达到高可用的要求。
一.安装:
建议选择编译安装的方式,可以添加其它模块
1、下载新版本,到官网复制下载链接
wget http://nginx.org/download/nginx-1.15.10.tar.gz
2、解压tar -zxvf nginx-1.15.10.tar.gz
3、编译安装
# 进入解压目录:
cd nginx-1.15.10/
# 配置并编译安装nginx:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic
ubuntu可能会出现错误:
./configure: error: C compiler cc is not found
解决办法为执行:`sudo apt-get -f install`命令。
该命令的含义是去补全那些缺少的软件。
再安装gcc
sudo apt-get install build-essential
安装完可查看版本:
gcc --version
sudo make
sudo make install
# 启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
# 查看nginx进程:
ps -ef|grep nginx
二.配置转发,贴上nginx.conf代码
1.nginx监听的端口不能被占用,http当然是80和https为443
2.注意网上copy的代码可能有空格,会导致配置文件错误
3.access_log logs/host.access.log;开启日志,可以查看访问日志和错误日志
4. add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
增加实际访问的ip和状态,便于调试查看,实际运行可以隐藏
#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;
upstream webserver {
server xx.xxx.xx.xx:443 max_fails=2 fail_timeout=10s;
server xxx.xx.xxx.xxx:443 max_fails=2 fail_timeout=10s;
}
upstream backend.example.com {
server xxx.com:443 max_fails=2 fail_timeout=10s;
server xxx.xxx.xx:443 max_fails=2 fail_timeout=10s;
}
server {
listen 443;
server_name xxxx.xxxxx.xx;
ssl on;
ssl_certificate /etc/ssl/xxx.pem;
ssl_certificate_key /etc/ssl/xxxxx.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#charset koi8-r;
access_log logs/host.access.log;
location / {
add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
proxy_pass https://webserver;
index index.html index.htm;
}
location /eexce {
add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
proxy_pass https://backend.example.com;
}
#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 {
# listen 80;
# server_name test.emeet.ai;
# rewrite ^(.*)$ https://$host$1 permanent;
# }
# 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;
# }
#}
}