Nginx基础入门

Posted 虹猫tomcat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx基础入门相关的知识,希望对你有一定的参考价值。

nginx ("engine x")是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,其并发能力在同类型的网页服务器中表现较好。


Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率,能经受高负载的考验,有报告表明能支持高达50000个并发连接数。


Nginx有几大功能:反向代理,负载均衡,动静分离和高可用。


反向代理


先提一下正向代理。


正向代理就是在客户端(浏览器)配置代理服务器,通过代理服务器进行互联网访问,人手一个代理服务器。




负载均衡


很多问题单个服务器解决不了,可以增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,就是负载均衡。


Nginx基础入门(一)


动静分离


为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。


Nginx基础入门(一)


大致了解Nginx可以做的事之后,接着介绍一下Nginx常用命令和配置文件。


1 使用Nginx操作命令前提条件,必须进入nginx的目录。


cd /usr/local/nginx/sbin


2 查看nginx版本号


./nginx -v


Nginx基础入门(一)


3 启动nginx


./nginx


4 关闭nginx


./nginx -s stop


5 重新加载nginx


./nginx -s reload


6 查看nginx状态


ps -ef | grep nginx


Nginx基础入门(一)


7 对外开放访问的端口


firewall-cmd --add-port=端口号/tcp --permanent //添加端口号firewall-cmd --reload //重启防火墙



8 查看已经开放的端口号


firewall-cmd --list-all


修改配置文件


cd /usr/local/nginx/conf/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;
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)全局块:配置服务器整体运行的配置指令

比如 worker_processes 1:处理并发数的配置


(2)events 块:影响 Nginx 服务器与用户的网络连接

比如 worker_connections 1024; 支持的最大连接数为 1024


(3)http 块,还包含两部分:

http全局块

server 块


配置实例一(反向代理)



Nginx基础入门(一)


1 在 windows 系统的 host 文件进行域名(www.zhulin.com,有的可能会显示未备案)和 ip(服务器主机ip) 对应关系的配置。


Nginx基础入门(一)


2 nginx 进行请求转发的配置(反向代理配置)


Nginx基础入门(一)


测试


Nginx基础入门(一)



配置实例二(反向代理)


效果:使用 nginx 反向代理,根据访问的路径跳转到不同端口的服务中

nginx 监听端口为 9001。


访问 http://139.196.16.241:9001/edu/ 直接跳转到 127.0.0.1:8080

访问 http://139.196.16.241:9001/vod/ 直接跳转到 127.0.0.1:8081



1 准备两个 tomcat 服务器,一个 8080 端口,一个 8081 端口,并开放9001端口:


Nginx基础入门(一)


Nginx基础入门(一)


2 创建文件夹和测试页面


Nginx基础入门(一)


Nginx基础入门(一)


3 配置nginx.conf文件


Nginx基础入门(一)


4 测试


Nginx基础入门(一)




以上是关于Nginx基础入门的主要内容,如果未能解决你的问题,请参考以下文章

Nginx Web 基础入门

Nginx基础入门之相关配置模板样例参考

JSP基础--JSP入门

Nginx入门到实战 - 基础篇

Nginx基础入门之文件操作优化及请求特殊处理介绍

Nginx基础入门之fastcgi模块常用配置项说明