Nginx快速入门
使用背景:
当我们用户量增多时,我们需要使用多个服务器
因此我们需要使用反向代理
我们的多台服务器型号不同时,我们可以通过负载均衡来协调
nginx内存少,并发能力强
正向代理和反向代理
正向代理:例如开一个v p n就可以访问goo狗。
反向代理:访问一个域名,即可访问所有的服务器资源
nginx的安装
1. Windows安装
http://nginx.org/en/download.html
下载后解压
点击conf目录下的nginx.conf修改端口号:
在浏览器中打开:
2. Linux安装
http://nginx.org/en/download.html
上传到服务器
解压文件:
进入文件夹输入:
再执行一次make命令
成功
开启: cd /usr/local/nginx/sbin
进入该文件夹输入:./nginx
成功访问!!!
nginx常用命令
常用命令:
cd /usr/local/nginx/sbin
进入控制目录
./nginx
启动
./nginx -s stop
停止
./nginx -s quit
安全退出
./nginx -s reload
重新加载配置文件
ps aux|grep nginx
查看nginx进程
Nginx演示
1. 后端准备
我们这里为了方便直接用若依的前后端不分离版本:
下载地址:http://ruoyi.vip/
导入数据库后配置:
运行:
成功!!
我们再重新运行一个端口:
再次运行:
也成功了!
2.nginx配置
打开nginx.config
- upstream:负载均衡配置
- location配置:
再server上添加一个upstream:
upstream demo {
server 127.0.0.1:9090 weight=1;
server 127.0.0.1:8089 weight=1;
}
在location里添加一个
proxy_pass http://demo;
在cmd里输入nginx -s reload
重新加载
再次运行:
代理成功!!!!!
#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;
# http
upstream demo {
server 127.0.0.1:9090 weight=1;
server 127.0.0.1:8089 weight=1;
}
server {
listen 8100;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://demo;
}
#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;
# }
#}
}