北京SEO

lnmp环境nginx 配置多虚拟主机例子

2019/10/10/17:45:02  阅读:1713  来源:谷歌SEO算法  标签: 5G标准出炉

配置多虚拟主机是每一台WEB服务器几乎会做的一个动作了,特别是我们这种小站长了,下文小编来为各位介绍lnmp环境nginx 配置多虚拟主机例子,希望例子可以帮助到大家.

1、首先进入 /usr/local/nginx/conf/ 目录(自己nginx安装的路径),刚编译好的nginx 在这个目录下是木有 vhost 目录的,创建好这个目录后,打开nginx.conf 文件,在 http 范围内添加 include vhost/*.conf,包含创建的虚拟主机配置文件,然后保存,创建虚拟目录共用的server文件,就是每个conf都会使用到的配置项,我们把他独立成一个模块供大家使用.

server.conf文件:

  1. location~.*\.(php|php5)?$
  2. {
  3. #fastcgi_passunix:/tmp/php-cgi.sock;
  4. fastcgi_pass127.0.0.1:9000;
  5. fastcgi_indexindex.php;
  6. includefastcgi.conf;
  7. }
  8. location~.*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
  9. {
  10. expires30d;
  11. #access_logoff;
  12. }
  13. location~.*\.(js|css)?$
  14. {
  15. expires15d;
  16. #access_logoff;
  17. }
  18. fastcgi_index:(nginx的默认首页文件)

如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中.

例如:

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;

fastcgi_pass:指定FastCGI服务器监听端口与地址,可以是本机或者其它.

用netstat -tln 查看端口使用情况:

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

可以看到9000端口处于监听状态:另外的其他fastcgi配置,放入fastcgi.conf公用配置文件中,server.conf 来包含他.

fastcgi.conf文件相关配置项:

  1. ?fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;#脚本文件请求的路径
  2. fastcgi_paramQUERY_STRING$query_string;#请求的参数;如?app=123
  3. fastcgi_paramREQUEST_METHOD$request_method;#请求的动作(GET,POST)
  4. fastcgi_paramCONTENT_TYPE$content_type;#请求头中的Content-Type字段
  5. fastcgi_paramCONTENT_LENGTH$content_length;#请求头中的Content-length字段。
  6. fastcgi_paramSCRIPT_NAME$fastcgi_script_name;#脚本名称
  7. fastcgi_paramREQUEST_URI$request_uri;#请求的地址不带参数
  8. fastcgi_paramDOCUMENT_URI$document_uri;#与$uri相同。
  9. fastcgi_paramDOCUMENT_ROOT$document_root;#网站的根目录。在server配置中root指令中指定的值
  10. fastcgi_paramSERVER_PROTOCOL$server_protocol;#请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  11. fastcgi_paramGATEWAY_INTERFACECGI/1.1;#cgi版本
  12. fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;#nginx版本号,可修改、隐藏
  13. fastcgi_paramREMOTE_ADDR$remote_addr;#客户端IP
  14. fastcgi_paramREMOTE_PORT$remote_port;#客户端端口
  15. fastcgi_paramSERVER_ADDR$server_addr;#服务器IP地址
  16. fastcgi_paramSERVER_PORT$server_port;#服务器端口
  17. fastcgi_paramSERVER_NAME$server_name;#服务器名,域名在server配置中指定的server_name
  18. #fastcgi_paramPATH_INFO$path_info;#可自定义变量
  19. #PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
  20. #fastcgi_paramREDIRECT_STATUS200;

2、准备好了公用文件server.conf 和 fastcgi.conf 后,进入vhost目录,之前手动创建的,创建虚拟主机.

  1. vimtest.conf;
  2. server
  3. {
  4. listen80;
  5. server_nametest.cn;
  6. indexindex.htmlindex.htmindex.php;
  7. root/var/www/test;
  8. access_log/var/www/logs/test.log;
  9. error_logoff;
  10. location/{
  11. try_files$uri$uri//index.php$uri?$args;
  12. }
  13. if(!-e$request_filename){
  14. rewrite^(.*)$/index.php?s=$1last;
  15. break;
  16. }
  17. includeserver.conf;
  18. }

test.conf 虚拟主机文件配置完成

重启nginx:

记得先验证测试后再重启,否则会出现nginx 重启不来.

测试:/usr/local/nginx/sbin/nginx -t

没问题后进行重启:

重启:/usr/local/nginx/sbin/nginx -s reload

如果没有域名来解析指定到自己的主机ip,可以直接把自己的主机ip 指到本地,编辑C:\Windows\System32\drivers\etc\hosts 文件就可,加入:

  1. <二、我的实例>
  2. ---------vhosts/led.conf
  3. server{
  4. listen80;
  5. server_namewww.led.com
  6. indexindex.htmlindex.htmindex.php;
  7. root/usr/local/vhost/led;
  8. #charsetkoi8-r;
  9. #access_loglogs/host.access.logmain;
  10. location/{
  11. root/usr/local/vhost/led;
  12. indexindex.htmlindex.htmindex.php;
  13. }
  14. #error_page404/404.html;
  15. #redirectservererrorpagestothestaticpage/50x.html
  16. #
  17. error_page500502503504/50x.html;
  18. location=/50x.html{
  19. roothtml;
  20. }
  21. #proxythePHPscriptstoApachelisteningon127.0.0.1:80
  22. #
  23. #location~\.php${
  24. #proxy_passhttp://127.0.0.1;
  25. #}
  26. location~.*\.(php|php5)?$
  27. {
  28. #fastcgi_passunix:/tmp/php-cgi.sock;
  29. fastcgi_pass127.0.0.1:9000;
  30. fastcgi_indexindex.php;
  31. fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
  32. includefastcgi_params;
  33. }
  34. location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
  35. {
  36. expires30d;
  37. }
  38. location~.*\.(js|css)?$
  39. {
  40. expires1h;
  41. }
  42. log_formataccess'$remote_addr-$remote_user[$time_local]"$request"'
  43. '$status$body_bytes_sent"$http_referer"'
  44. '"$http_user_agent"$http_x_forwarded_for';
  45. }
  46. ----------------nginx.conf
  47. usernginxnginx;
  48. worker_processes8;
  49. error_loglogs/error.log;
  50. #error_loglogs/error.lognotice;
  51. #error_loglogs/error.loginfo;
  52. pid/usr/local/nginx/nginx.pid;
  53. worker_rlimit_nofile65535;
  54. events{
  55. useepoll;
  56. worker_connections65535;
  57. }
  58. http{
  59. includemime.types;
  60. default_typeapplication/octet-stream;
  61. #log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
  62. #'$status$body_bytes_sent"$http_referer"'
  63. #'"$http_user_agent""$http_x_forwarded_for"';
  64. #access_loglogs/access.logmain;
  65. server_names_hash_bucket_size128;
  66. client_header_buffer_size32k;
  67. large_client_header_buffers432k;
  68. client_max_body_size8m;
  69. sendfileon;
  70. tcp_nopushon;
  71. #keepalive_timeout0;
  72. keepalive_timeout65;
  73. tcp_nodelayon;
  74. fastcgi_connect_timeout300;
  75. fastcgi_send_timeout300;
  76. fastcgi_read_timeout300;
  77. fastcgi_buffer_size64k;
  78. fastcgi_buffers464k;
  79. fastcgi_busy_buffers_size128k;
  80. fastcgi_temp_file_write_size128k;
  81. gzipon;//phpfensi.com
  82. gzip_min_length1k;
  83. gzip_buffers416k;
  84. gzip_http_version1.0;
  85. gzip_comp_level2;
  86. gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
  87. gzip_varyon;
  88. server{
  89. listen80;
  90. server_name_;
  91. server_name_in_redirectoff;
  92. location/{
  93. root/usr/share/nginx/html;
  94. indexindex.html;
  95. }
  96. }
  97. #包含所有的虚拟主机的配置文件
  98. include/usr/local/nginx/conf/vhosts/*;
  99. }

广告内容

lnmp环境nginx 配置多虚拟主机例子 lnmp环境nginx 配置多虚拟主机例子 lnmp环境nginx 配置多虚拟主机例子

相关阅读

热门评论

小潘seo 小潘seo

重庆小潘seo博客和你一起学习SEO知识,共同分享SEO优化~

总篇数165

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法