本文已参与「新人创作礼」活动,一起开启掘金创作之路。
nginx的配置文件注解版
nginx语法小坑:每一行语法最后需要一个英文的分号(;)
nginx.conf
###[全局块]配置区
#######用户组
user nginx;
#######允许生成worker processes数
worker_processes 1;
#######日志存放路径
error_log /var/log/nginx/error.log warn;
#######进程pid存放路径
pid /var/run/nginx.pid;
###[事件块]配置区
events {
#######允许网络最大连接数
worker_connections 1024;
#######每个进程的最大连接数
#######选取事件驱动模型处理连接请求
#######是否允许同时接收多个网路连接
#######开启网络连接序列化
####### ...
}
###[http块]配置区
http {
####### include引入文件内容 mime.types 内容 资源的媒体类型库文件。 浏览器的文件资源是通过 Content-Type 来表示的。mime.types 文件内容具体如下面内容
include /etc/nginx/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 /var/log/nginx/access.log main;
######################################################################################################################
##sendfile是个比 read 和 write 更高性能的系统接口, 不过需要注意的是,sendfile 是将 in_fd 的内容发送到 out_fd 。
##而 in_fd 不能是 socket , 也就是只能文件句柄。 所以当 Nginx 是一个静态文件服务器的时候,开启 SENDFILE 配置项能大大提高 Nginx 的性能。
##但是当 Nginx 是作为一个反向代理来使用的时候,SENDFILE 则没什么用了,因为 Nginx 是反向代理的时候。
##in_fd 就不是文件句柄而是 socket,此时就不符合 sendfile 函数的参数要求
######################################################################################################################
####### Nginx 是一个静态文件服务器的时候可以开启提升性能
sendfile on;
#tcp_nopush on;
#######连接超时时间
keepalive_timeout 65;
#######是否开启压缩
#gzip on;
####### 负载均衡配置 upstream配置上有服务器
####### include引入conf.d目录下的配置文件
#######通常server块配置放置于此
#######[server块]配置区
include /etc/nginx/conf.d/*.conf;
}
###server块]配置区
server {
####### 监听端口
listen 80;
####### 监听域:端口
listen [::]:80;
####### 监听域名下的服务
server_name localhost;
####### 访问日志
#access_log /var/log/nginx/host.access.log main;
#######
location / {
####### 根文件夹
root /usr/share/nginx/html;
####### 默认首页
index index.html index.htm;
}
####### 配置404页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
########配置500页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#######方向代理的配置
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#######PHP环境的配置
# 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;
#}
}
mime.types 内容:
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/wasm wasm;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
server 的 location:
location /static/ {
root /usr/share/nginx/html;
}
意思:访问路径为 域名或者ip访问进来(xxxx.xx/static)访问访问 /usr/share/nginx/html 下的资源内容