老实说,从Android开发转到后端开发,有些基础概念仍是比较模糊的,特别是对一些框架的熟悉。其中,Nginx
算一个,因而忽然有了想搞懂Nginx
的冲动。。。php
nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 26.34% busiest sites in June 2019. Here are some of the success stories: Dropbox, Netflix, Wordpress.com, FastMail.FM.html
释义:Nginx
是一个HTTP服务器、反向代理服务器、邮件代理服务器、TCP/UDP代理服务器。java
说到这里,就会有个疑问,和咱们一般使用的Tomcat
有何区别?nginx
Tomcat
和Nginx
的应用场景不一样。Nginx是开源、高性能的HTTP服务器,而Tomcat更可能是一种容器,做为Web服务器处理Java Servlet、JSP等。正则表达式
## 安装指令
brew install nginx
## 安装目录
cd /usr/local/etc/nginx
复制代码
Nginx很重要的一环就是配置文件,学习配置文件的格式以及如何使用每一个配置是基础。apache
#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 {
# we’re on a Solaris-based system and have determined that nginx
# will stop responding to new requests over time with the default
# connection-processing mechanism, so we switch to the second-best
use /dev/poll;
# the product of this number and the number of worker_processes
# indicates how many simultaneous connections per IP:port pair are accepted
worker_connections 2048;
}
复制代码
http {
## include文件
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;
## 文件I/O指令,该指令使用sendfile(2)直接复制数据从一个到另外一个文件描述符
sendfile on;
## 仅依赖于sendfile使用,Nginx在一个数据包中尝试发送响应头以及在数据包中发送一个完整的文件
#tcp_nopush on;
## 该指令指定keep-alive链接持续多久。
#keepalive_timeout 0;
keepalive_timeout 65;
## 开启gzip压缩
#gzip on;
}
复制代码
server_name
指令逻辑分割的资源。server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
## location 指令能够用在虚拟服务器 server 部分,井且意味着提供来自客户端的 URI 或 者内部重定向访问。
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;
#}
}
复制代码
能够看出,Nginx的配置是模块化的,全局配置负责各个方面,HTTP Server、虚拟服务器则分模块配置,每一个server_name
单独生效。后端
一、www.nginx.cn/doc/general… 二、精通Nginx(第二版) 三、examples.javacodegeeks.com/enterprise-…tomcat