nginx学习笔记1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyf837368104/article/details/7627033

接下来咱们就开始学习nginx的配置文件吧

然后cd到etc/nginx目录下

第一部分  nginx.conf

主配置文件为nginx.conf

与php相关的是fastcgi_params

与python相关的是uwsgi_params

nginx的注意事项1:恩,首先我们先确保先把apache停了,因为Apache默认使用80端口,两个会抢的!

我们首先可以打开nginx.conf

#使用的用户和组
user www-data;
#指定工作衍生进程数(默认为CPU的线程数,咱的是N2600,双核四线程!)
worker_processes 4;
#指定pid存放的路径,应该记录了nginx守护进程的进程号,是其他进程的父进程id!(如Apache下有PidFile )
pid /var/run/nginx.pid;
events {
use epoll;#linux下性能最好的event模式!默认木有这条!
#允许的连接数
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 测试多次请求传输之间的时间,再一次持久链接,如果客户端的两次HTTP请求超过keepalive_timeout的时间,则关闭链接释放资源!!(Apache下也有)
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
#啥是mime?我们的计算机网络里貌似提到了,但咱得搞懂!
#
default_type application/octet-stream;
#当用户请求的文件木有在服务器中定义mime类型映射,使用DefaultType
#可以有啥? text/html text/plain(表示后缀为txt的文件以文本显示) image/gif
#这里的application/octet-stream 是啥?
#若未定的文件多是软件或者图像,建议为application/octet-stream
#浏览器会提示用户以“另存为”的方式保存文件!
##
# Logging Settings
##
#日志,详细见后面
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##开启gzip压缩
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
##很好,我们可以清楚的看到,我们会加载conf.d目录与sites-enabled目录下得所有文件,这也证明了我们为啥子可以在这两个目录下写配置文件!
}
#nginx同样可以做邮件代理服务器!
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

第二部分  nginx的虚拟主机配置

啥是虚拟主机?话说我刚进精弘的任务就是配置虚拟主机,当时觉得好神奇啊!哈哈!首先虚拟主机是把一台主机分成一台台“虚拟”的主机,你想想我有20几个站点,咱不是大款,木有20几台服务器,于是利用虚拟主机,就可以解决这个问题!

如下图是最简单的虚拟主机配置文件


与Apache相同,nginx也可以配置多种类型的虚拟主机。

  • 基于域名的虚拟主机
  • 基于IP的虚拟主机
  • 基于端口的虚拟主机

然后我们发现目录下有site-available与site-enabled两个目录,和Apache一模一样,我们一般采用的方法是在前者下写好配置文件,到后者目录下做好一个软连接!原因如同目录的名字一样,前者是存在的网站,而后者是正在使用的目录!nginx默认会加载site-enabled目录!前者的目录下有一个default给我们参考如何写虚拟主机的配置文件

让我们来看一下这段:

# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file
#这段文字很清晰告诉我们如何添加虚拟主机!
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
##上面这段讲了具体可以参考那些网站和例子!
server {
#开始了直接server打头,如果配置在nginx.conf里,请写在http{……server{……}}里,咱们是基于http协议的!
#listen 80; ## listen for ipv4; this line is default and implied
#监听80端口!
#listen [::]:80 default ipv6only=on; ## listen for ipv6
#root HTML网页文件存放的目录
root /usr/share/nginx/www;
#默认首页文件,顺序从左往右!如果找不到index.html,就去找index.htm`
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
#主机名称
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
#显而易见,访问根目录,直接跳转到首页!
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex off;
}
#这里涉及了404,50x的页面及其地址
#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 /usr/share/nginx/www;
#}
# 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$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# 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;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}

我们可以发现要配多个虚拟主机基本的格式是

server{……}

server{……}

然后我们来看一下nginx的日志

第三部分  nginx的日志

与nginx日志相关的指令主要有两条

log_format #设置日志的格式

access_log #指定日志文件的存放路径,格式,缓存大小

两条指令即可放在http{……}或者server{……}之间

log_format XXXX(日志的名字,不能重复!)
	'$remote_addr [$time_local] '
	'"$request" $status $body_bytes_sent '
	'"$http_referer" "$http_user_agent" '
	'$http_x_forwarded_for';
$remote_addr		 #反向代理服务器IP地址
$time_local		 #用于记录访问时间与时区
$request 		 #用于记录请求的URL与HTTP协议
$status			 #用于记录请求状态
$body_bytes_sent 	 #发送给客户端的文件主题内容大小
$http_referer		 #记录从那个页面链接访问过来的
$http_user_agent	 #用于记录客户端浏览器的相关信息
$http_x_forwarded_for    #记录客户端IP地址和客户端请求的服务器地址(这是由于是反向代理服务器,web服务器无法的IP)直接拿到客户端

access_log

access_log path [format {buffer=size | off}]

#不使用日志

access_log off

#使用默认XXXX格式的日志

access_log /data/logs/file.log XXXX

 

 

然后来一个实际的例子,这是我们精弘网络的测试服务器上的nginx下的一段虚拟主机配置文件!

#ServerName bbs.zjut.com
#ServerAlias v.zjut.com bbs2.zjut.com bbs.zjut.edu.cn
#DocumentRoot /opt/www/bbs
#jump to bbs.zjut.com
#server{ listen 80;
# server_name bbs2.zjut.com v.zjut.com ;
# rewrite ^/(.*)$ http://bbs.zjut.com/$1 permanent;
# access_log off;
#}
#jump to bbs.zjut.in
server{ listen 80;
#监听80端口
server_name bbs2.zjut.in ;
#主机名称
rewrite ^/(.*)$ http://bbs.zjut.in/$1 permanent;
access_log off;
#日志关闭
}
#bbs.zjut.com
server{ listen 80;
#监听80端口
server_name bbs.zjut.in;
root /opt/www/bbs;
#目录放在/opt/www/bbs
index index.html index.htm index.php;
#先找index.html,然后index.htm,再是index.php
log_format bbs_log '$remote_addr [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$http_x_forwarded_for';
#定义日志格式
# access_log /opt/log/nginx/bbs/bbs.log bbs_log;
error_page 404 = /errorpage/404.html;
#404错误页面的地址
error_page 500 502 503 504 = /errorpage/500.html;
#50X错误地址的目录
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{ expires 30d; }
location ~ .*\.(js|css|asp|aspx)?$
{ expires 1h; }
location /UploadFile {return 404;}
location /uploadfile {return 404;}
# location ~ .*\.(sh|bash|asp)?$
# { return 403; }
#location ~* \.(html|htm|xml|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|flv|txt|exe|zip|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$
#{ access_log off;
# include /etc/nginx/conf.d/ipdeny.conf;
#}
location ~ .*\.(php|php5)?$ {
#反向代理到本机的8000端口,我们可以去Apache的site-enabled目录下发现该站在Apache监听的就是8000端口!
proxy_pass http://127.0.0.1:8000;
# access_log off;
# include /etc/nginx/conf.d/ipdeny.conf;
}
# error_page 403 http://whatwhat.xxx/sss.html
}

关于nginx的日志,还是很不了解,希望在接下来的系列学习文章有所体现。


猜你喜欢

转载自blog.csdn.net/zyf837368104/article/details/7627033