nginx学习笔记1

来源:互联网 发布:淘宝里的金牌卖家 编辑:程序博客网 时间:2024/06/08 13:35

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

然后cd到etc/nginx目录下

第一部分  nginx.conf

主配置文件为nginx.conf

与php相关的是fastcgi_params

与python相关的是uwsgi_params

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

我们首先可以打开nginx.conf

[html] view plaincopy
  1. #使用的用户和组  
  2. user www-data;  
  3. #指定工作衍生进程数(默认为CPU的线程数,咱的是N2600,双核四线程!)  
  4. worker_processes 4;  
  5. #指定pid存放的路径,应该记录了nginx守护进程的进程号,是其他进程的父进程id!(如Apache下有PidFile )  
  6. pid /var/run/nginx.pid;  
  7. events {  
  8. use epoll;#linux下性能最好的event模式!默认木有这条!  
  9. #允许的连接数  
  10. worker_connections 768;  
  11. # multi_accept on;  
  12. }  
  13. http {  
  14. ##  
  15. # Basic Settings  
  16. ##  
  17. sendfile on;  
  18. tcp_nopush on;  
  19. tcp_nodelay on;  
  20. #keepalive_timeout 测试多次请求传输之间的时间,再一次持久链接,如果客户端的两次HTTP请求超过keepalive_timeout的时间,则关闭链接释放资源!!(Apache下也有)  
  21. keepalive_timeout 65;  
  22. types_hash_max_size 2048;  
  23. # server_tokens off;  
  24. # server_names_hash_bucket_size 64;  
  25. # server_name_in_redirect off;  
  26. include /etc/nginx/mime.types;  
  27. #啥是mime?我们的计算机网络里貌似提到了,但咱得搞懂!  
  28. #  
  29. default_type application/octet-stream;  
  30. #当用户请求的文件木有在服务器中定义mime类型映射,使用DefaultType  
  31. #可以有啥? text/html text/plain(表示后缀为txt的文件以文本显示) image/gif  
  32. #这里的application/octet-stream 是啥?  
  33. #若未定的文件多是软件或者图像,建议为application/octet-stream  
  34. #浏览器会提示用户以“另存为”的方式保存文件!  
  35. ##  
  36. # Logging Settings  
  37. ##  
  38. #日志,详细见后面  
  39. access_log /var/log/nginx/access.log;  
  40. error_log /var/log/nginx/error.log;  
  41. ##  
  42. # Gzip Settings  
  43. ##开启gzip压缩  
  44. gzip on;  
  45. gzip_disable "msie6";  
  46. # gzip_vary on;  
  47. # gzip_proxied any;  
  48. # gzip_comp_level 6;  
  49. # gzip_buffers 16 8k;  
  50. # gzip_http_version 1.1;  
  51. # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;  
  52. ##  
  53. # Virtual Host Configs  
  54. ##  
  55. include /etc/nginx/conf.d/*.conf;  
  56. include /etc/nginx/sites-enabled/*;  
  57. ##很好,我们可以清楚的看到,我们会加载conf.d目录与sites-enabled目录下得所有文件,这也证明了我们为啥子可以在这两个目录下写配置文件!  
  58. }  
  59. #nginx同样可以做邮件代理服务器!  
  60. #mail {  
  61. # # See sample authentication script at:  
  62. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript  
  63. #  
  64. # # auth_http localhost/auth.php;  
  65. # # pop3_capabilities "TOP" "USER";  
  66. # # imap_capabilities "IMAP4rev1" "UIDPLUS";  
  67. #  
  68. # server {  
  69. # listen localhost:110;  
  70. # protocol pop3;  
  71. # proxy on;  
  72. # }  
  73. #  
  74. # server {  
  75. # listen localhost:143;  
  76. # protocol imap;  
  77. # proxy on;  
  78. # }  
  79. #}  

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

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

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


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

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

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

让我们来看一下这段:

[html] view plaincopy
  1. # You may add here your  
  2. # server {  
  3. # ...  
  4. # }  
  5. # statements for each of your virtual hosts to this file  
  6. #这段文字很清晰告诉我们如何添加虚拟主机!  
  7. ##  
  8. # You should look at the following URL's in order to grasp a solid understanding  
  9. # of Nginx configuration files in order to fully unleash the power of Nginx.  
  10. # http://wiki.nginx.org/Pitfalls  
  11. # http://wiki.nginx.org/QuickStart  
  12. # http://wiki.nginx.org/Configuration  
  13. #  
  14. # Generally, you will want to move this file somewhere, and start with a clean  
  15. # file but keep this around for reference. Or just disable in sites-enabled.  
  16. #  
  17. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.  
  18. ##  
  19. ##上面这段讲了具体可以参考那些网站和例子!  
  20. server {  
  21. #开始了直接server打头,如果配置在nginx.conf里,请写在http{……server{……}}里,咱们是基于http协议的!  
  22. #listen 80; ## listen for ipv4; this line is default and implied  
  23. #监听80端口!  
  24. #listen [::]:80 default ipv6only=on; ## listen for ipv6  
  25. #root HTML网页文件存放的目录  
  26. root /usr/share/nginx/www;  
  27. #默认首页文件,顺序从左往右!如果找不到index.html,就去找index.htm`  
  28. index index.html index.htm;  
  29. # Make site accessible from http://localhost/  
  30. server_name localhost;  
  31. #主机名称  
  32. location / {  
  33. # First attempt to serve request as file, then  
  34. # as directory, then fall back to index.html  
  35. try_files $uri $uri/ /index.html;  
  36. }  
  37. #显而易见,访问根目录,直接跳转到首页!  
  38. location /doc {  
  39. root /usr/share;  
  40. autoindex on;  
  41. allow 127.0.0.1;  
  42. deny all;  
  43. }  
  44. location /images {  
  45. root /usr/share;  
  46. autoindex off;  
  47. }  
  48. #这里涉及了404,50x的页面及其地址  
  49. #error_page 404 /404.html;  
  50. # redirect server error pages to the static page /50x.html  
  51. #  
  52. #error_page 500 502 503 504 /50x.html;  
  53. #location = /50x.html {  
  54. # root /usr/share/nginx/www;  
  55. #}  
  56. # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  57. #  
  58. #location ~ \.php$ {  
  59. # proxy_pass http://127.0.0.1;  
  60. #}  
  61. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  62. #  
  63. #location ~ \.php$ {  
  64. # fastcgi_pass 127.0.0.1:9000;  
  65. # fastcgi_index index.php;  
  66. # include fastcgi_params;  
  67. #}  
  68. # deny access to .htaccess files, if Apache's document root  
  69. # concurs with nginx's one  
  70. #  
  71. #location ~ /\.ht {  
  72. # deny all;  
  73. #}  
  74. }  
  75. # another virtual host using mix of IP-, name-, and port-based configuration  
  76. #  
  77. #server {  
  78. # listen 8000;  
  79. # listen somename:8080;  
  80. # server_name somename alias another.alias;  
  81. # root html;  
  82. # index index.html index.htm;  
  83. #  
  84. # location / {  
  85. # try_files $uri $uri/ /index.html;  
  86. # }  
  87. #}  
  88. # HTTPS server  
  89. #  
  90. #server {  
  91. # listen 443;  
  92. # server_name localhost;  
  93. #  
  94. # root html;  
  95. # index index.html index.htm;  
  96. #  
  97. # ssl on;  
  98. # ssl_certificate cert.pem;  
  99. # ssl_certificate_key cert.key;  
  100. #  
  101. # ssl_session_timeout 5m;  
  102. #  
  103. # ssl_protocols SSLv3 TLSv1;  
  104. # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;  
  105. # ssl_prefer_server_ciphers on;  
  106. #  
  107. # location / {  
  108. # try_files $uri $uri/ /index.html;  
  109. # }  
  110. #}  

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

server{……}

server{……}

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

第三部分  nginx的日志

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

log_format #设置日志的格式

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

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

[html] view plaincopy
  1. log_format XXXX(日志的名字,不能重复!)  
  2.     '$remote_addr [$time_local] '  
  3.     '"$request" $status $body_bytes_sent '  
  4.     '"$http_referer" "$http_user_agent" '  
  5.     '$http_x_forwarded_for';  
  6. $remote_addr         #反向代理服务器IP地址  
  7. $time_local      #用于记录访问时间与时区  
  8. $request         #用于记录请求的URL与HTTP协议  
  9. $status          #用于记录请求状态  
  10. $body_bytes_sent     #发送给客户端的文件主题内容大小  
  11. $http_referer        #记录从那个页面链接访问过来的  
  12. $http_user_agent     #用于记录客户端浏览器的相关信息  
  13. $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下的一段虚拟主机配置文件!

[html] view plaincopy
  1. #ServerName bbs.zjut.com  
  2. #ServerAlias v.zjut.com bbs2.zjut.com bbs.zjut.edu.cn  
  3. #DocumentRoot /opt/www/bbs  
  4. #jump to bbs.zjut.com  
  5. #server{ listen 80;  
  6. # server_name bbs2.zjut.com v.zjut.com ;  
  7. # rewrite ^/(.*)$ http://bbs.zjut.com/$1 permanent;  
  8. # access_log off;  
  9. #}  
  10. #jump to bbs.zjut.in  
  11. server{ listen 80;  
  12. #监听80端口  
  13. server_name bbs2.zjut.in ;  
  14. #主机名称  
  15. rewrite ^/(.*)$ http://bbs.zjut.in/$1 permanent;  
  16. access_log off;  
  17. #日志关闭  
  18. }  
  19. #bbs.zjut.com  
  20. server{ listen 80;  
  21. #监听80端口  
  22. server_name bbs.zjut.in;  
  23. root /opt/www/bbs;  
  24. #目录放在/opt/www/bbs  
  25. index index.html index.htm index.php;  
  26. #先找index.html,然后index.htm,再是index.php  
  27. log_format bbs_log '$remote_addr [$time_local] '  
  28. '"$request" $status $body_bytes_sent '  
  29. '"$http_referer" "$http_user_agent" '  
  30. '$http_x_forwarded_for';  
  31. #定义日志格式  
  32. # access_log /opt/log/nginx/bbs/bbs.log bbs_log;  
  33. error_page 404 = /errorpage/404.html;  
  34. #404错误页面的地址  
  35. error_page 500 502 503 504 = /errorpage/500.html;  
  36. #50X错误地址的目录  
  37. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$  
  38. { expires 30d; }  
  39. location ~ .*\.(js|css|asp|aspx)?$  
  40. { expires 1h; }  
  41. location /UploadFile {return 404;}  
  42. location /uploadfile {return 404;}  
  43. # location ~ .*\.(sh|bash|asp)?$  
  44. # { return 403; }  
  45. #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)$  
  46. #{ access_log off;  
  47. # include /etc/nginx/conf.d/ipdeny.conf;  
  48. #}  
  49. location ~ .*\.(php|php5)?$ {  
  50. #反向代理到本机的8000端口,我们可以去Apache的site-enabled目录下发现该站在Apache监听的就是8000端口!  
  51. proxy_pass http://127.0.0.1:8000;  
  52. # access_log off;  
  53. # include /etc/nginx/conf.d/ipdeny.conf;  
  54. }  
  55. # error_page 403 http://whatwhat.xxx/sss.html  
  56. }  

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


转载地址http://blog.csdn.net/zyf837368104/article/details/7627033

0 0
原创粉丝点击