当前位置:   article > 正文

Nginx 多server配置_新增nginx server

新增nginx server
nginx 支持laravel配置
  1. server {
  2. listen 80;
  3. server_name *.dev.laravel;
  4. if ($host ~ ^(.*)\.dev\.laravel$ )
  5. {
  6. set $app $1;
  7. set $path /data/www/learn/laravel/$1/public;
  8. }
  9. root $path;
  10. keepalive_timeout 0;
  11. client_max_body_size 1000m;
  12. location / {
  13. index default.php index.php index.html index.htm;
  14. try_files $uri $uri/ /index.php?$query_string;
  15. }
  16. location ~ \.php/?.*$ {
  17. fastcgi_pass 127.0.0.1:9000;
  18. fastcgi_index index.php;
  19. include fastcgi_params;
  20. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  21. }
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. error_log /var/log/nginx/dev.laravel.error.log;
  27. access_log off;
  28. }
zend_framework nginx配置
  1. server {
  2. listen 80;
  3. server_name servername.com;
  4. root /data/www/zendapp/public;
  5. location / {
  6. index index.php;
  7. }
  8. # Deny access to sensitive files.
  9. location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
  10. deny all;
  11. }
  12. location ~ \.htaccess {
  13. deny all;
  14. }
  15. # Rewrite rule adapted from zendapp/public/.htaccess
  16. if (!-e $request_filename) {
  17. rewrite ^.*$ /index.php last;
  18. }
  19. #图片/js/css不显示解决
  20. location ~* ^.+\.(js|ico|gif|jpg|jpeg|pdf|png|css)$ {
  21. access_log off;
  22. expires 7d;
  23. }
  24. # PHP scripts will be forwarded to fastcgi processess.
  25. # Remember that the `fastcgi_pass` directive must specify the same
  26. # port on which `spawn-fcgi` runs.
  27. location ~ \.php$ {
  28. include fastcgi_params;
  29. fastcgi_pass 127.0.0.1:9000;
  30. fastcgi_index index.php;
  31. }
  32. location = /50x.html {
  33. root /var/www/default;
  34. }
  35. }

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

 

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

  1. server {
  2. listen 80;
  3. server_name *.dev.tp5;
  4. if ($host ~ ^(.*)\.dev\.tp5$ )
  5. {
  6. set $app $1;
  7. set $path /data/www/$1;
  8. }
  9. root $path;
  10. location / {
  11. index index.htm index.html index.php;
  12. #访问路径的文件不存在则重写URL转交给ThinkPHP处理
  13. if (!-e $request_filename) {
  14. rewrite ^/(.*)$ /index.php/$1 last;
  15. break;
  16. }
  17. }
  18. location ~ \.php/?.*$ {
  19. fastcgi_pass 127.0.0.1:9000;#php-fpm调用listen
  20. fastcgi_index index.php;
  21. #加载Nginx默认"服务器环境变量"配置
  22. include fastcgi.conf;
  23. #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量
  24. set $fastcgi_script_name2 $fastcgi_script_name;
  25. if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
  26. set $fastcgi_script_name2 $1;
  27. set $path_info $2;
  28. }
  29. #项目命名空间设置
  30. fastcgi_param PATH_INFO $path_info;
  31. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
  32. fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
  33. }
  34. #网站单独的日志文件
  35. error_log /var/log/nginx/dev.tp5.error.log;
  36. access_log /var/log/nginx/dev.tp5.access.log main;
  37. }
nginx支持YII配置
  1. server
  2. {
  3. listen 80;
  4. server_name *.dev.yii;
  5. index index.php;
  6. if ($host ~ ^(.*)\.dev\.yii$ )
  7. {
  8. set $app $1;
  9. set $path /data/www/$1;
  10. set $webpath $path/htdocs;
  11. }
  12. root $webpath;
  13. if (!-e $request_filename) {
  14. rewrite ^.*$ /index.php last;
  15. }
  16. location ~ \.(htm|html|js|css|gif|jpg|png|swf|ico|woff|eot|ttf|svg)$
  17. {
  18. root $webpath;
  19. }
  20. location / {
  21. fastcgi_pass 127.0.0.1:9000;
  22. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  23. fastcgi_index index.php;
  24. include fastcgi_params;
  25. }
  26. error_log /var/log/nginx/dev.yii.error.log;
  27. }

nginx多server

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

  1. user www;
  2. worker_processes 2;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. debug_connection 127.0.0.1;
  8. debug_connection 192.168.1.0/24;
  9. }
  10. http {
  11. include /etc/nginx/mime.types;
  12. # ......
  13. server {
  14. listen 80;
  15. server_name localhost;
  16. location / {
  17. root /data/www/; # 该项要修改为你准备存放相关网页的路径
  18. index index.html index.htm home.php default.php index.php;
  19. }
  20. #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  21. location ~ \.php$ {
  22. fastcgi_pass 127.0.0.1:9000;
  23. fastcgi_index index.php;
  24. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  25. include /usr/local/etc/nginx/fastcgi.conf;
  26. }
  27. }
  28. include /etc/nginx/servers/*.conf;
  29. }

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

 

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

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

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

 

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

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

  1. server
  2. {
  3. listen 80;
  4. server_name ~^(.+)?\.ssdr\.info$;
  5. index index.html;
  6. if ($host = ssdr.info){
  7. rewrite ^ http://www.ssdr.info permanent;
  8. }
  9. if ($host ~ ^(.*)\.ssdr\.info$ )
  10. set $domain $1;
  11. }
  12. root /data/wwwsite/ssdr.info/$domain/;
  13. }

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

  1. /data/wwwsite/ssdr.info/www/
  2. /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指令将其赋值给一个命名的变量
  1. server
  2. {
  3. listen 80;
  4. server_name ~^(.+)?\.sklinux\.com$;
  5. set $www_root $1;
  6. root /data/www/sklinux.com/$www_root/;
  7. location ~ .*\.php?$ {
  8. fastcgi_pass 127.0.0.1:9000;
  9. fastcgi_index index.php;
  10. fastcgi_param SCRIPT_FILENAME /data/www/sklinux.com/$fastcgi_script_name;
  11. include fastcgi_params;
  12. }
  13. }
nginx 无法加载css/js/gif/png等图片等文件
  1. location ~ .*\.(js|css|png|jpg|gif)$ {
  2. root /home/xiaxt_web/wordpress; #站点根目录
  3. if (-f $request_filename) {
  4. expires 1d;
  5. break;
  6. }
  7. }
如何解决nginx上传大文件的限制:打开nginx主配置文件nginx.conf,一般在/usr/local/nginx/conf/nginx.conf这个位置,找到http{}段,修改或者添加
client_max_body_size 30m; 
 30m表示最大上传30M,需要多大设置多大。然后重启nginx
nginx 禁止对.svn 目录访问
  1. location ~* /(\.svn|CVS|Entries){
  2. deny all;
  3. }
 nginx内置变量
  1. $args #请求中的参数值
  2. $query_string #同 $args
  3. $arg_NAME #GET请求中NAME的值
  4. $is_args #如果请求中有参数,值为"?",否则为空字符串
  5. $uri #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"
  6. $document_uri #同 $uri
  7. $document_root #当前请求的文档根目录或别名
  8. $host #优先级:HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名
  9. $hostname #主机名
  10. $https #如果开启了SSL安全模式,值为"on",否则为空字符串。
  11. $binary_remote_addr #客户端地址的二进制形式,固定长度为4个字节
  12. $body_bytes_sent #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
  13. $bytes_sent #传输给客户端的字节数
  14. $connection #TCP连接的序列号
  15. $connection_requests #TCP连接当前的请求数量
  16. $content_length #"Content-Length" 请求头字段
  17. $content_type #"Content-Type" 请求头字段
  18. $cookie_name #cookie名称
  19. $limit_rate #用于设置响应的速度限制
  20. $msec #当前的Unix时间戳
  21. $nginx_version #nginx版本
  22. $pid #工作进程的PID
  23. $pipe #如果请求来自管道通信,值为"p",否则为"."
  24. $proxy_protocol_addr #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
  25. $realpath_root #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
  26. $remote_addr #客户端地址
  27. $remote_port #客户端端口
  28. $remote_user #用于HTTP基础认证服务的用户名
  29. $request #代表客户端的请求地址
  30. $request_body #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
  31. $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
  32. $request_completion #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
  33. $request_filename #当前连接请求的文件路径,由root或alias指令与URI请求生成
  34. $request_length #请求的长度 (包括请求的地址,http请求头和请求主体)
  35. $request_method #HTTP请求方法,通常为"GET""POST"
  36. $request_time #处理客户端请求使用的时间; 从读取客户端的第一个字节开始计时
  37. $request_uri #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
  38. $scheme #请求使用的Web协议,"http""https"
  39. $server_addr #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
  40. $server_name #服务器名
  41. $server_port #服务器端口
  42. $server_protocol #服务器的HTTP版本,通常为 "HTTP/1.0""HTTP/1.1"
  43. $status #HTTP响应代码
  44. $time_iso8601 #服务器时间的ISO 8610格式
  45. $time_local #服务器时间(LOG Format 格式)
  46. $cookie_NAME #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
  47. $http_NAME #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
  48. $http_cookie
  49. $http_post
  50. $http_referer
  51. $http_user_agent
  52. $http_x_forwarded_for
  53. $sent_http_NAME #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
  54. $sent_http_cache_control
  55. $sent_http_connection
  56. $sent_http_content_type
  57. $sent_http_keep_alive
  58. $sent_http_last_modified
  59. $sent_http_location
  60. $sent_http_transfer_encoding
 
 
 
 
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/732504
推荐阅读
相关标签
  

闽ICP备14008679号