Nginx 快速入门_1:概念、安装、常用命令与配置文件说明
本教程100%与翻墙无关,仅设计服务器的部署与nginx的相关知识,供学习交流使用
一、基础概念
1.1反向代理
(1)正向代理:正向代理类似一个跳板机,代理访问外部资源
用户a想要访问网站域名c,必须通过一个域名为b的代理服务器访问
即a->b(代理服务器)->c(想要访问的服务器,如百度)
(2)反向代理:类似与一个网关。
我的理解是:反向代理的服务器,是内外服务器组对外暴露的接口,用户只能请求这个接口,但最后具体在内部怎跳转,是由反向代理服务器决定的,而不是用户决定的。
概念请参考:反向代理和正向代理区别.
1.2负载均衡
将相同的应用部署到多台机器上。 解决访问统一入口问题,我们可以在集群前面增加负载均衡设备,实现流量分发。 负载均衡(Load Balance),意思是将负载(工作任务,访问请求)进行平衡、分摊到多个操作单元(服务器,组件)上进行执行。
简单来说,就是把n个客户端请求,安装均衡算法,分发给服务器集群中的各个服务器上。
1.3动静分离
- 静态资源: 当用户多次访问这个资源,资源的源代码永远不会改变的资源。 如css,html,js
- 动态资源:当用户多次访问这个资源,资源的源代码可能会发送改变。如jsp、servlet
- 动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路 .
- 动静分离简单的概括是:动态文件与静态文件的分离。
二、Nginx的安装
2.1安装教程
Centos 安装Nginx:https://cloud.tencent.com/developer/article/1924770
安装时报错处理:https://blog.csdn.net/weixin_43916065/article/details/107710383
测试是否安装成功:https://www.cnblogs.com/monjeo/p/7570117.html
2.2nginx常用命令
前提:进入以下目录
[root@VM-4-8-centos sbin]# cd /usr/local/nginx/sbin
查看版本
[root@VM-4-8-centos sbin]# ./nginx -v
查看当前nginx 的状态
[root@VM-4-8-centos sbin]# ps -ef |grep nginx
停止
[root@VM-4-8-centos sbin]# ./nginx -s stop
启动
[root@VM-4-8-centos sbin]# ./nginx
重加载(当/usr/local/nginx/conf 中的nginx.conf发生变化时,需要对nginx重加载)
注:这个不是重启
[root@VM-4-8-centos sbin]# ./nginx -s reload
2.3 nginx的配置文件
/usr/local/nginx/conf 中的nginx.conf 就是nginx的配置文件
配置文件结构:
除去注释,配置文件由3部分组成
(1).全局块:
从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令
注:worker_processes 1;worker_processes越大,支持的并发处理的数目也就越多
#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;
(2)events块
主要用于配置Nginx服务器与用户网络连接
注:worker_connections 支持的最大连接数
events {
worker_connections 1024;
}
(3)http块/网络块
nginx中配置最频繁的部分
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;
# }
#}
}
http块分为:http全局块与server块
http全局块
http全局块配置的指令包括:文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限
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块
server块与虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机完成一样,该技术的产生是为了节省互联网服务器硬件成本。
每个http块可以包括多个server块,而每个server块相当于一个虚拟主机。
每个server块也分为全局server块,以及可以同时包含多个的location块。
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;
# }
#}
其他人的可参考笔记:https://blog.csdn.net/weixin_43508544/article/details/102997779