nginx的配置文件nginx.conf
一、nginx 的配置文件
nginx 的配置文件 nginx.conf 文件可以分为以下三部分:
全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数, 进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。比如 worker_processes 1 是 Nginx 服务器并发处理服务的关键配置, worker_processes 值越大,可以支持的并发处理量越多,但是会受到硬件、软件设备的制约。一般不会对全局快的内容进行修改。
event 块
event 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对 work process 下的网络进行序列化,是否允许同时接收多个网络,选取哪种驱动模型来处理连接请求,每个 work process 可以同时支持的最大连接数等。
如worker_connections 1024:表示每个 work process 的最大连接数为 1024。
一般不会对event块的内容进行修改。
http块
这一部分是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
http块又分为 http全局块 和 server 块。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
server块
这块和虚拟主机有密切联系,虚拟主机从用户角度看,和一台独立的硬件主机是一样。而 http块可以有多个 server块,每一个 server块就相当于一个虚拟主机。每一个 server块 又可以分为 全局server块 以及多个 location块。
全局 server 块
最常见的配置就是本虚拟机主机的监听配置和本虚拟机的名称或 IP 配置。
location 块
这块的主要作用就是基于 Nginx 服务器接收到的请求字符串(例如:server_name/uri-string),对虚拟机主机名称(也可以是IP别名)之外的字符串(如前面的:/uri-string)进行匹配。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里执行。
我们主要的操作是以server块为单位完成的。
首先,我们应该明白一个server块就对应一个虚拟机或者一个工程,然后我们逐条分析里面的内容含义。
listen 80; 表示监听哪个端口
server_name localhost; 表示监听哪个主机
而localhost 则是对监听的进一步扩展,比如 localhost /dev/ 表示监听localhost:80/dev/端口
root html;
index index.html index.htm;
proxy_pass http:192.168.131.21:8088/dev/;
而这三者则表示起始页跟默认转发地址
那么按照上面的配置
server{
listen 80;
server_name localhost;
location /dev/ {
root html;
index index.html index.htm;
proxy_pass http:192.168.131.21:8088/dev/;
}
}
则表示监听localhost:80/dev/,并将请求转发给 http:192.168.131.21:8088/dev/。
同时我们也可以这么写
server{
listen 80;
server_name localhost;
location /dev/ {
root html;
index index.html index.htm;
proxy_pass http://myserver/dev/;
}
upstream myserver{
server 192.168.134.105:8080 weight=8;
server 192.168.134.105:8081 weight=10;
}
则表示过来的请求会转发给192.168.134.105:8080跟192.168.134.105:8081这两个的其中一个,几率则是根据weight的大小来决定(weight越大几率被转发的几率越大,weight若是不写则weight默认值都是1)。
那么前后端分离项目应该是这样:
location / {
root html/dist;
index index.html index.htm;
}
location /dev-api {
proxy_pass localhost:8080
}
当然修改完成配置文件不要忘了重启一下
./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 {
proxy_intercept_errors on;
fastcgi_intercept_errors on;
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 60004;
server_name renzheng.ambow.com;
return 301 https://$server_name:60001$request_uri;
}
server {
listen 60001 ssl default_server;
server_name renzheng.ambow.com;
ssl_certificate ../cert/ambow.com.pem;
ssl_certificate_key ../cert/ambow.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:60002;
}
}
server {
listen 60002;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ {
proxy_pass http://127.0.0.1:8080/;
}
#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;
# }
#}
}