Nginx简介:
Nginx因其特有的稳定性,丰富的资源模块,灵活的配置较低的资源消耗而闻名,其特点是占有内存少,并发能力强,是一款轻量级的HTTP和反向代理服务器。
1. 下载
官网链接: http://nginx.org/en/download.html.
一般下载稳定版(Stable version),解压后如图:
2.修改端口
1.cmd进到安装目录, 执行nginx.exe
2.浏览器访问 => http://localhost:82/ 会进入到nginx UI界面
3.启动后修改配置文件,要让他生效
D:\Application\nginx-1.20.2>nginx -t
nginx: the configuration file D:\Application\nginx-1.20.2/conf/nginx.conf syntax is ok
nginx: configuration file D:\Application\nginx-1.20.2/conf/nginx.conf test is successful
D:\Application\nginx-1.20.2>nginx -s reload
3.配置文件
修改配置文件,每句后面要加分号,每次都要重新加载配置文件。
//全局配置
#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 {
//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;
//负载均衡的配置
upsteram xxx {
}
//代理的配置
server {
listen 81;
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;
# }
#}
}
4.正向代理
正向代理是代理客户端
5.反向代理
反向代理则是代理服务器,来接受Internet的连接请求,然后将请求分发给各服务器,并将服务器的结果返回给客户机,客户端是无感知的。
反向代理,优点有哪些:
- 1.保护和隐藏原始资源服务器
- 2.加密和SSL加速
- 3.通过缓存静态资源,加速web请求
- 4.实现负载均衡
5.1负载均衡
1.需求:访问localhost:81 =>轮询 localhost:8080 和 localhost:8081
2.修改nginx.conf
#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 nolanStudy{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://nolanStudy;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location /data-pipeline {
root html;
#xxxxxxx
}
}
# 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;
# }
#}
}
5.2加权轮询
//根据服务器配置加权重
upstream nolanStudy{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=5;
}
6.redis做session共享
7.动静分离
#整合动静分离
upstream static_photo {
server xxxx:80;
}
upstream java {
server xxxx:81;
}
server {
listen 80;
server_name ds.com;
access_log /nginx_log/lb_ds_access.log main;
location / {
root /web/ds;
index index.html;
}
location ~* .*\.(jpg|png|gif)$ {
proxy_pass http://static_photo;
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* .jsp$ {
proxy_pass http://java;
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
8.停止nginx
1.停止
D:\Application\nginx-1.20.2>nginx.exe -s stop
2.安全退出
D:\Application\nginx-1.20.2>nginx.exe -s quit
9.报警
1.报警10013: An attempt was made to access a socket in a way forbidden by its access permissions
Solution:配置文件中的port被占用,要么将进程杀死,要么换port
找到占用端口的pid
netstat -ano | findstr :80
杀死进程
taskkill -pid xxx -F