`
hudeyong926
  • 浏览: 2017869 次
  • 来自: 武汉
社区版块
存档分类
最新评论

Nginx 多server配置

阅读更多
nginx 支持laravel配置
server {
    listen       80;
    server_name  *.dev.laravel;
    if ($host ~ ^(.*)\.dev\.laravel$ )  
    {
        set $app  $1; 
        set $path /data/www/learn/laravel/$1/public;  
    } 
    root $path;
    keepalive_timeout   0;
    client_max_body_size    1000m;

    location / {
        index default.php index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php/?.*$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    error_log /var/nginx/dev.laravel.error.log;  
    access_log off;
}
zend_framework nginx配置
server {
  listen 80;
  server_name servername.com;
  root /data/www/zendapp/public;
  location / {
    index index.php;
  }

  # Deny access to sensitive files.
  location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
    deny all;
  }
  location ~ \.htaccess {
    deny all;
  }

  # Rewrite rule adapted from zendapp/public/.htaccess
  if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
  }
  #图片/js/css不显示解决
  location ~* ^.+\.(js|ico|gif|jpg|jpeg|pdf|png|css)$ {
    access_log   off;
    expires      7d;
  }
  # PHP scripts will be forwarded to fastcgi processess.
  # Remember that the `fastcgi_pass` directive must specify the same
  # port on which `spawn-fcgi` runs.
  location ~ \.php$ {
    include        fastcgi_params;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
  }

  location = /50x.html {
      root   /var/www/default;
  }
}

Nginx 不支持 Apache 的 .htaccess 文件,所以需要在 Nginx 配置文件中编写重写规则。Apache 的绝大部分 RewriteRule 命令都可以不做修改的放到 Nginx 中直接使用。你只要把 RewriteRule 改成 rewrite,[L] 改成 last 之类的就可以了,具体可以看一下 Nginx 的 Rewrite 文档。

 

Nginx部署ThinkPHP项目的办法网上通用解决方法的配置如下

server {
    listen 80;
    server_name *.dev.tp5;
    if ($host ~ ^(.*)\.dev\.tp5$ )
    {
        set $app     $1;
        set $path    /data/www/$1;
    }
    root $path;
    location / {
        index  index.htm index.html index.php;
        #访问路径的文件不存在则重写URL转交给ThinkPHP处理
        if (!-e $request_filename) {
           rewrite  ^/(.*)$  /index.php/$1  last;
           break;
        }
    }
    location ~ \.php/?.*$ {
        fastcgi_pass   127.0.0.1:9000;#php-fpm调用listen
        fastcgi_index  index.php;
        #加载Nginx默认"服务器环境变量"配置
        include        fastcgi.conf;
        
        #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量
        set $fastcgi_script_name2 $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
            set $fastcgi_script_name2 $1;
            set $path_info $2;
        }

        #项目命名空间设置
        fastcgi_param   PATH_INFO $path_info;
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;
        fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;
    }
    #网站单独的日志文件
    error_log /var/log/nginx/dev.tp5.error.log;
    access_log /var/log/nginx/dev.tp5.access.log main;
}
nginx支持YII配置
server
{
    listen 80;
    server_name *.dev.yii;
    index index.php;

    if ($host ~ ^(.*)\.dev\.yii$ )
    {
        set $app     $1;
        set $path    /data/www/$1;
        set $webpath $path/htdocs;
    }
    root $webpath;
    if (!-e $request_filename) {
     	rewrite ^.*$ /index.php last;
    }

    location ~ \.(htm|html|js|css|gif|jpg|png|swf|ico|woff|eot|ttf|svg)$
    {
        root $webpath;
    }
    location / {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params;
    }
    error_log /var/nginx/dev.yii.error.log;
    access_log /var/nginx/dev.yii.access.log main;
}

nginx多server

1.检查/etc/nginx/nginx.conf配置文件,确保文件中有:include /etc/nginx/servers/*.conf;  

user www;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    debug_connection 127.0.0.1;
    debug_connection 192.168.1.0/24;
}
http {
    include       /etc/nginx/mime.types;
    #  ......
    server {
        listen       80;
        server_name  localhost;

        location / {
            root /data/www/; # 该项要修改为你准备存放相关网页的路径
            index  index.html index.htm home.php default.php index.php;
        }
    
        #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;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include /usr/local/etc/nginx/fastcgi.conf;
        }
    }
    include /etc/nginx/servers/*.conf;
}

 2.关键步骤,在目录/etc/nginx/servers/下面新建文件site1.conf,site2.conf,文件名任意写,自己看明白就OK,后缀名需要与步骤1配置的一致,这里为.conf

 

在一个server块中配置多个站点.server_name指令的正则表达式应用

server 
{ 
    listen 80; 
    server_name ~^(www\.)?(.+)$; 
    index index.php index.html; 
    root /data/wwwsite/$2; 
} 
 站点的主目录应该类似于这样的结构:
/data/wwwsite/ssdr.info 
/data/wwwsite/linuxtone.org 
/data/wwwsite/baidu.com 

本例中正则表达式捕获组创建了变量,普通的正则表达式捕获组可以创建$0、$1、…、$9这10个变量。$0表示原串,$1-$9表示第一到第九个匹配组的内容。

 

在一个server块中为一个站点配置多个二级域名 。

实际网站目录结构中我们通常会为站点的二级域名独立创建一个目录,同样我们可以使用正则的捕获来实现在一个server块中配置多个二级域名:

server 
{ 
    listen 80; 
    server_name ~^(.+)?\.ssdr\.info$; 
    index index.html; 
    if ($host = ssdr.info){
    	rewrite ^ http://www.ssdr.info permanent;
    } 
    if ($host ~ ^(.*)\.ssdr\.info$ )
       set $domain $1; 
    } 
    root /data/wwwsite/ssdr.info/$domain/; 
}

站点的目录结构应该如下:

/data/wwwsite/ssdr.info/www/ 
/data/wwwsite/ssdr.info/nginx/ 

这样访问www.ssdr.info时root目录为/data/wwwsite/ssdr.info/www/,nginx.ssdr.info时为/data/wwwsite/ssdr.info/nginx/,以此类推。后面if语句的作用是将ssdr.info的方位重定向到www.ssdr.info,这样既解决了网站的主目录访问,又可以增加seo中对www.ssdr.info的域名权重。注意: 通配符名字只可以在名字的起始处或结尾处包含一个星号,并且星号与其他字符之间用点分隔。所以,“www.*.example.org”和“w*.example.org”都是非法的。

多个正则表达式
果你在server_name中用了正则,而下面的location字段又使用了正则匹配,这样将无法使用$1,$2这样的引用,解决方法是通过set指令将其赋值给一个命名的变量
server
{
	 listen      80;
	 server_name ~^(.+)?\.sklinux\.com$;
	 set $www_root $1;
	 root /data/www/sklinux.com/$www_root/;
	 location ~ .*\.php?$ {
		 fastcgi_pass   127.0.0.1:9000;
		 fastcgi_index  index.php;
		 fastcgi_param  SCRIPT_FILENAME /data/www/sklinux.com/$fastcgi_script_name;
		 include        fastcgi_params;
	 }
}
nginx 无法加载css/js/gif/png等图片等文件
location ~ .*\.(js|css|png|jpg|gif)$ {
    root /home/xiaxt_web/wordpress; #站点根目录
    if (-f $request_filename) {
        expires 1d;
        break;
    }
}
如何解决nginx上传大文件的限制:打开nginx主配置文件nginx.conf,一般在/usr/local/nginx/conf/nginx.conf这个位置,找到http{}段,修改或者添加
client_max_body_size 30m; 
 30m表示最大上传30M,需要多大设置多大。然后重启nginx
nginx 禁止对.svn 目录访问
location ~* /(\.svn|CVS|Entries){
    deny all;
}
 nginx内置变量
$args                    #请求中的参数值
$query_string            #同 $args
$arg_NAME                #GET请求中NAME的值
$is_args                 #如果请求中有参数,值为"?",否则为空字符串
$uri                     #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"。
$document_uri            #同 $uri
$document_root           #当前请求的文档根目录或别名
$host                    #优先级:HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名
$hostname                #主机名
$https                   #如果开启了SSL安全模式,值为"on",否则为空字符串。
$binary_remote_addr      #客户端地址的二进制形式,固定长度为4个字节
$body_bytes_sent         #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$bytes_sent              #传输给客户端的字节数
$connection              #TCP连接的序列号
$connection_requests     #TCP连接当前的请求数量
$content_length          #"Content-Length" 请求头字段
$content_type            #"Content-Type" 请求头字段
$cookie_name             #cookie名称
$limit_rate              #用于设置响应的速度限制
$msec                    #当前的Unix时间戳
$nginx_version           #nginx版本
$pid                     #工作进程的PID
$pipe                    #如果请求来自管道通信,值为"p",否则为"."
$proxy_protocol_addr     #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
$realpath_root           #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
$remote_addr             #客户端地址
$remote_port             #客户端端口
$remote_user             #用于HTTP基础认证服务的用户名
$request                 #代表客户端的请求地址
$request_body            #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
$request_body_file       #将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off,uwsgi_pass_request_body off,or scgi_pass_request_body off
$request_completion      #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
$request_filename        #当前连接请求的文件路径,由root或alias指令与URI请求生成
$request_length          #请求的长度 (包括请求的地址,http请求头和请求主体)
$request_method          #HTTP请求方法,通常为"GET"或"POST"
$request_time            #处理客户端请求使用的时间; 从读取客户端的第一个字节开始计时
$request_uri             #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
$scheme                  #请求使用的Web协议,"http" 或 "https"
$server_addr             #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
$server_name             #服务器名
$server_port             #服务器端口
$server_protocol         #服务器的HTTP版本,通常为 "HTTP/1.0" 或 "HTTP/1.1"
$status                  #HTTP响应代码
$time_iso8601            #服务器时间的ISO 8610格式
$time_local              #服务器时间(LOG Format 格式)
$cookie_NAME             #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
$http_NAME               #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
$http_cookie
$http_post
$http_referer
$http_user_agent
$http_x_forwarded_for
$sent_http_NAME          #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location
$sent_http_transfer_encoding
 
 
 
 
 
分享到:
评论

相关推荐

    Nginx端口映射配置方法

    前因 搭建一个网站传统的方法是使用一个web服务器去解析文件入口文件,如使用Nginx,Apache解析到对应的入口文件,但是随着技术的发展,...Nginx端口映射配置 server { listen 80; server_name rbac.dev-lu.com; #

    python-nginx, 在 python 中,创建和修改 Nginx serverblock配置.zip

    python-nginx, 在 python 中,创建和修改 Nginx serverblock配置 python 用于在 python ( 包含评论) 中轻松创建和修改 Nginx serverblock配置的模块。安装pip install python-nginx示例创建 Nginx serverb

    详细nginx多域名配置的方法

    Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,nginx多域名配置是在配置文件中建立多个server配置,在每个server配置中用server_name来对域名信息进行过滤。 实现方法如下: 举个例子,下面是一...

    nginx 多站点配置,本地能访问,不能访问的解决

    通过阿里云服务器的nginx配置多个端口指向不同网站时,出现本地能打开,不能打开的问题,后分析解决,分享给大家.

    FASTDFS.rar

    fastDFS-5.0.4.jar 和 storage_server tracker_server ,nginx_server 配置明细,fastDFS-5.0.4.jar 和 storage_server tracker_server ,nginx_server 配置明细,fastDFS-5.0.4.jar 和 storage_server tracker_...

    Nginx DNS resolver配置实例

    主要介绍了Nginx DNS resolver配置实例,本文讲解在proxy_pass 和 upstream server 通信的时候需要手动指定 resolver,本文就给出了配置实例,需要的朋友可以参考下

    详解Nginx虚拟主机配置中server_name的具体写法

    server_name指令可以设置基于域名的虚拟主机,根据请求头部的内容,一个ip的服务器可以配置多个域名。下面这些server_name的参数是有效的: server_name jb51.net; server_name jb51.net www.jb51.net; server_name ...

    nginx 配置跨域失效修复的方法示例

    nginx 配置跨域不生效 如下配置 server { listen 80; server_name localhost; # 接口转发 location /api/ { # 允许请求地址跨域 * 做为通配符 add_header 'Access-Control-Allow-Origin' '*'; # 设置请求...

    FastDFS+Nginx安装及配置

    FastDFS+Nginx 安装及配置。FastDFS 是一个开源的轻量级分布式文件系统,纯 C 实现,支持 Linux、FreeBSD 等 UNIX 系统,FastDFS 可以看做是基于文件的 key value pair 存储系统,包含两个角色,分别为跟踪 服务器...

    windows server 2012 R2服务器下配置php7+nginx1.1环境

    这是本人亲自在腾讯云服务器上windows server 2012 R2 64位系统上搭建的php7+nginx1.1的环境。全程截图,详细记录下来了。希望对大家有帮助。

    与DNS配置有关的nginx服务器配置

    3. 配置Nginx服务器,使得网站支持443端口的 https 加密数据传输,可以通过https://erp.example.com访问,并返回 html 代码 “<h1> erp.example.com </h1>” 4. 在服务器上安装部署Tomcat服务器 5.资源只包含实验...

    Windows下Nginx配置SSL实现Https访问(包含证书生成)

    Windows下Nginx配置SSL实现Https访问(包含证书生成)

    nginx for windows 傻瓜配置

    nginx.exe右键打开[就闪一下,是正常的,不要多次打开] 可以在任务管理器看到有两个nginx.exe进程,那就正常了 ----------------创建新的网站------------------------ 只需要conf/vhosts.conf最后一行 复制黏贴一份 ...

    windows服务器部署 nginx+tomcat+mysql服务器端部署 阿里云服务器部署及配置

    详细说明了windows服务器nginx+tomcat+mysql部署及配置(配置阿里云后台安全组,配置域名)很适合新手学习 附件中包含: 1.操作说明文档 2.操作录屏 3.安装所用到的软件安装包 1)Windows Server 2019 数据中心版 ...

    Nginx同时支持Http和Https的配置详解

    现在的网站支持Https几乎是标配功能,Nginx能很好的支持Https功能。下面列举一个配置同时...废话不多说,下面直接贴一个Nginx支持Http和Https的配置,是我的wordpres网站支持Https的配置,大家何以参考。 server { #

    服务器Nginx配置.docx

    Nginx配置文件采用层级结构,包括main、events、http块,以及在http块内的server块。 设置worker进程: 在events块中,可以设置worker进程的数量,通常设置为CPU核心数。 配置HTTP服务器: 在http块中,可以配置...

    nginx 多站点配置方法集合

    那么我们开始吧: 1、为我们的站点创建配置文件 我是这么做的,在nginx的配置文件conf目录下创建一个专门存放VirtualHost的目录,命名为vhosts_conf,可以把虚拟目录的配置全部放在这里。在里面创建名为vhosts_...

    Web服务器三剑客运维配置实战 Nginx+JVM+Tomcat+HTTP协议.zip

    ├─5.05 tomcat运维-server.xml配置文件注释.mp4 ├─5.06 tomcat运维-web站点部署.mp4 ├─5.07 tomcat运维-部署开源站点.mp4 ├─5.08 tomcat运维-tomcat优化.mp4 ├─5.09 tomcat运维-tomcat性能优化.mp4 ├─...

    Nginx安装配置、Resin安装配置说明文档

    a)在server1 上安装配置 nginx + nginx_upstream_jvm_route - 22 - b)分别在两台机器上 安装 resin - 22 - c)配置两台机器 的 resin - 23 - d)整合 ngxin resin - 24 - e)测试,打开浏览器,输入 ...

Global site tag (gtag.js) - Google Analytics