1 概述
有时候项目中需要统一数据库配置地址,但是做政府项目很多是内网环境,数据库的地址无法统一,但是又不好每个环境单独配置数据库,这时候可以使用nginx代理成统一的数据库地址。
1.1 下载ngnx
没啥好说的 自行百度下载解压关键是配置信息,目前本人用的是nginx-1.16.1版本的下载链接
1.2 配置
需要在解压后的nginx.conf文件的同级目录下创建一个mycon文件夹,并创建db.conf文件
db.conf配置
server {
#注意端口不能跟mysql监听的一样
listen 3307;
proxy_pass db;
}
# 负载的实际端口和地址 也可以其他能访问的地址,不一定是本机
upstream db {
server 127.0.0.1:3306;
}
nginx完整配置
千万不能放在http标签里面,stream 和http 标签同级
#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;
}
# 数据库代理 这个就是代理的配置 其他的就是默认的
stream{
include myconf/db.conf;
}
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;
# }
#}
}
1.3 nginx重启暂停命令
- 启动:双击nginx.exe
- 快速停止或关闭Nginx:
nginx.exe -s stop
- 配置文件修改重装载命令:
nginx.exe -s reload