Chinaunix首页 | 论坛 | 博客
  • 博客访问: 424925
  • 博文数量: 58
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 623
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-26 18:48
个人简介

在生存面前,那纯洁的理想,原来是那么脆弱不堪!

文章分类

全部博文(58)

文章存档

2022年(1)

2021年(1)

2019年(3)

2018年(6)

2017年(6)

2016年(14)

2015年(10)

2014年(16)

2013年(1)

我的朋友

分类: 系统运维

2016-01-15 15:59:03

Nginx默认的配置文件,虽然可以工作,但是只能支持静态网页,而且需要把静态页面放在html目录下。通过修改配置选项,可以实现灵活功能。
1,先看默认的配置文件。
  1. worker_processes 1;
  2. events {
  3.     worker_connections 1024;
  4. }
  5. http {
  6.     include mime.types;
  7.     default_type application/octet-stream;
  8.     sendfile on;
  9.     keepalive_timeout 65;
  10.     server {
  11.         listen 80;
  12.         server_name localhost;
  13.         location / {
  14.             root html;
  15.             index index.html index.htm;
  16.         }
  17.         error_page 500 502 503 504 /50x.html;
  18.         location = /50x.html {
  19.             root html;
  20.         }
  21.     }
  22. }
这个配置文件跟C语言风格很像。结构可以理解为四层:
    1)服务进程。worker_processes 1; 服务器的工作进程分配。

    2)http{}  这里包含http协议的功能配置。

    3)server{} 构造其中一个web服务的功能配置。nginx可以添加多个web服务,且监听不同端口。

    4)location{} 位于web服务中。匹配url中的路径。


2,修改配置。
    1)加大进程数,worker_processes。
        这里主要以web服务器的硬件配置和软件服务的特性。一般进程数和cpu核数对等。如果软件服务有大量的阻塞式I/OF访问,可以加大进程数。
        修改运行用户。user  nginx;  如果监听80端口,则需要root用户启动,随后会chroot到普通用户,降低权限。

    2)http{} 中不需要有太大的修改。可以添加https服务。把默认的配置打开。

  1. #server {
  2.     # listen 443 ssl;
  3.     # server_name localhost;

  4.     # ssl_certificate cert.pem;
  5.     # ssl_certificate_key cert.key;

  6.     # ssl_session_cache shared:SSL:1m;
  7.     # ssl_session_timeout 5m;

  8.     # ssl_ciphers HIGH:!aNULL:!MD5;
  9.     # ssl_prefer_server_ciphers on;

  10.     # location / {
  11.     # root html;
  12.     # index index.html index.htm;
  13.     # }
  14.     #}
    3)server{} 中可以修改监听端口。(如 listen 8080)
        
    4)server_name域名配置 。nginx依托域名匹配的方法完成虚拟服务器的配置。
         在不同的server{} 中,设置不同的server_name ,对于客户端的请求,nginx会匹配http请求头中的 Host:
        
    5)location 配置url路径分离。       

        在location 和{} 之间可以添加一些参数,分离web请求。
        比较类型: =    与url完全匹配。
                         ~    大小写敏感的匹配。
                         ~*  大小写不敏感的匹配。
                         ^~  匹配url的前半部分。

例如:首先匹配php  ,默认指向html目录。

  1. location ~ \.php$ {
  2.    proxy_pass http://127.0.0.1;
  3. }

  4. location ~ /{
  5.    root   html;
  6.    index  index.html index.htm;
  7. }
        其中root指定的是跟路径。index指定的是目录默认的页面。

阅读(3671) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~