Hazx小屋 Hazx小屋

LNMP下Nginx的简单配置

in 服务器相关 阅读: 1634 文章转载请注明来源!

  这里简单举例和介绍一下LNMP(Linux+Nginx+MySQL+PHP)下Nginx的基本配置。
  首先你需要知道你的Nginx安装路径,一般在/usr/local/nginx/下,如果你是使用《EZHTTP》安装的LNMP,默认路径也在此。配置文件在/usr/local/nginx/conf/下,虚拟主机配置文件在/usr/local/nginx/conf/vhost/下。配置Nginx主要涉及conf/nginx.conf文件和vhost/下的所有.conf文件。

主配置文件nginx.conf

  主配置文件nginx.conf位于nginx/conf/下,以下给出一个范例:【文章结尾有下载(推荐)】

error_log  /home/wwwlog/nginx/error.log  error ;        #这里的路径需要存在
pid logs/nginx.pid;
user  www;
worker_processes  auto;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  51200;
}


http {
    client_body_buffer_size 32k;
    client_header_buffer_size 2k;
    client_max_body_size 2m;
    default_type application/octet-stream;
    log_not_found off;
    server_tokens off;
    include       mime.types;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types       text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/xml application/rss+xml application/xhtml+xml application/atom_xml;
    gzip_vary on;
    #error_page   500 502 503 504  /50x.html; 
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';

    server {
        listen 80 default_server;
        server_name www.hmacg.cn hmacg.cn localhost;        #绑定的域名,多个域名用空格隔开
        root /home/WEB/hmacg.cn/;                           #指定到网站目录
        index index.php index.html index.htm;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
            include        fastcgi_params;
        }

    }
    #======== 如果默认网站不开https则删除此段
    server {
        listen 443 default_server;
        server_name www.hmacg.cn hmacg.cn localhost;       #绑定的域名,多个域名用空格隔开
        root /home/WEB/hmacg.cn/;                          #指定到网站目录
            index index.php index.html index.htm;

        ssl on;
        ssl_certificate /usr/local/nginx/conf/ssl.crt;        #指定SSL证书的CRT文件
        ssl_certificate_key /usr/local/nginx/conf/ssl.key;    #指定SSL证书的私钥KEY文件

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
            include        fastcgi_params;
        }
    }
    #=================

    include vhost/*.conf;
}

简单解释:
  主配置文件负责Nginx的主要配置信息以及默认网站的配置信息,默认网站也就是访问IP时可以打开的网站。我个人习惯将默认网站的目录指向一个单独的目录,其中放置一个空白的index.html,使默认网站打开是白页,这样在一些方面可能会有一些便利,比如同一主机上备案其他域名时避免管局访问IP审查服务器默认网站。
  error_log 指定一个存放错误日志的路径。如果是通过《EZHTTP》安装的则无需修改。
  listen 80 default_server; 监听80端口,默认网站。固定格式。当然如果你想监听其他端口也可以修改。
  server_name 绑定的域名,多个域名用空格隔开。如果不给默认网站绑定域名则留一个localhost即可。(注意这里仅指在主配置文件中。)
  root 网站目录的决定路径。
  index 默认网页文件,一般无需修改。
  fastcgi_pass 这里是指将对php文件的请求通过9000端口发给PHP处理。如果是通过《EZHTTP》安装的则无需修改。LNMP和LANMP此处都会有所不同。
  如果默认网站需要开启https,则完整复制粘贴一份server段,将80端口改成443,并增加SSL证书相关的三行基本配置。

虚拟主机配置文件

  虚拟主机配置文件位于nginx/conf/vhost/目录下,需要自行创建,以下给出两个范例:【文章结尾有下载(推荐)】
  仅http网站配置:↓↓↓

server {
    listen 80;
    server_name ex2.hmacg.cn;                    #绑定的域名,多个域名用空格隔开
    root /home/WEB/ex2.hmacg.cn/;                #指定到网站目录
    index index.php index.html index.htm;
    error_page 403 /__ErrorFiles__/403.html;    #错误页面,下同
    error_page 404 /__ErrorFiles__/404.html;
    error_page 500 /__ErrorFiles__/500.html;
    error_page 501 /__ErrorFiles__/501.html;
    error_page 502 /__ErrorFiles__/502.html;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires      6h;
    }

    location ~ .*\.(js|css)?$ {
            expires      6h;
    }

    access_log  /dev/null access;                                    #此处一般无需修改,如果需要记录所有成功日志则指定一个可写路径。
    error_log  /home/wwwlog/ex2.hmacg.cn/error_nginx.log error;        #此处需要指定一个可写路径保存错误日志。

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }  
}

  仅https网站配置:↓↓↓

server {
    listen 443;
    server_name ex3.hmacg.cn;                    #绑定的域名,多个域名用空格隔开
    root /home/WEB/ex3.hmacg.cn/;                #指定到网站目录
    index index.php index.html index.htm;
    error_page 403 /__ErrorFiles__/403.html;    #错误页面,下同
    error_page 404 /__ErrorFiles__/404.html;
    error_page 500 /__ErrorFiles__/500.html;
    error_page 501 /__ErrorFiles__/501.html;
    error_page 502 /__ErrorFiles__/502.html;

    ssl on;
    ssl_certificate /usr/local/nginx/conf/ssl.crt;        #指定SSL证书的CRT文件
    ssl_certificate_key /usr/local/nginx/conf/ssl.key;    #指定SSL证书的私钥KEY文件

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires      6h;
    }

    location ~ .*\.(js|css)?$ {
            expires      6h;
    }

    access_log  /dev/null access;                                    #此处一般无需修改,如果需要记录所有成功日志则指定一个可写路径。
    error_log  /home/wwwlog/ex3.hmacg.cn/error_nginx.log error;        #此处需要指定一个可写路径保存错误日志。

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE        open_basedir=$document_root:/tmp/:/proc/;
        include        fastcgi_params;
    }  
}

简单解释:
  虚拟主机配置文件本身不存在,如果你需要在一个服务器上绑定多个域名、开多个网站时就要手工创建它。虚拟主机配置文件在nginx/conf/vhost/目录下,文件名随意,以.conf结尾。为了管理方便,我比较推荐每个网站创建一个配置文件,并以域名命名配置文件,当这个网站需要绑定多个域名时以主域名命名。例如:exp.com网站的配置文件为“exp.com.conf”,其中绑定了域名“exp.com”和“www.exp.com”。
  listen 监听端口,http为80,https为443,若想监听其他特殊端口也可修改。
  server_name 绑定域名,多个域名用空格隔开。
  root 指定网站的绝对路径。
  error_page 指定错误页面,这里是相对于root的路径。
  expires 这里是指定静态文件在客户机上的缓存时间。切忌缓存php等动态文件以及html。
  access_log 成功访问的日志,一般填写路径为/dev/null即为不记录。
  error_log 错误日志。
  ssl_certificate 指定SSL证书的CRT文件。
  ssl_certificate_key 指定SSL证书的私钥KEY文件。
  如果网站需要同时开启http和https,将两个实例合在一个配置文件里即可。

配置文件实例下载(推荐)

  上面的代码可能因为没有行号而产生少许误导,所以推荐下载配置文件实例。【下载配置文件实例】

文章二维码

扫描二维码,在手机上阅读!

weblinuxnginx
最后由Hazx修改于2017-11-15 12:23
博客系统已萌萌哒运行了
© 2024 Hazx. Theme by 泽泽社长.
前篇 后篇
雷姆
拉姆