Nginx(二)-- 配置文件之虚拟主机配置

2020-05-28 00:00:00 文件 配置 虚拟主机 新建 夹中


1.配置文件与解释

  1. #user nobody;

  2. worker_processes 1; # 设置工作子进程,默认是1个工作子进程,可以修改,一般设置为CPU的总核数



  3. #error_log logs/error.log;

  4. #error_log logs/error.log notice;

  5. #error_log logs/error.log info;


  6. #pid logs/nginx.pid;



  7. events {

  8. worker_connections 1024; # 设置一个工作子进程大允许多少个连接

  9. }



  10. http {

  11. include mime.*; # 能够支持的类型

  12. default_type application/octet-stream;


  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '

  14. # '$status $body_bytes_sent "$http_referer" '

  15. # '"$http_user_agent" "$http_x_forwarded_for"';


  16. #access_log logs/access.log main;


  17. sendfile on;

  18. #tcp_nopush on;


  19. #keepalive_timeout 0;

  20. keepalive_timeout 65;


  21. #gzip on;


  22. server { # 虚拟主机段

  23. listen 80;

  24. server_name localhost;


  25. #charset koi8-r;


  26. #access_log logs/host.access.log main;


  27. location / {

  28. root html;

  29. index index.html index.htm;

  30. }


  31. #error_page 404 /404.html;


  32. # redirect server error pages to the static page /50x.html

  33. #

  34. error_page 500 502 503 504 /50x.html;

  35. location = /50x.html {

  36. root html;

  37. }


  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80

  39. #

  40. #location ~ \.php$ {

  41. # proxy_pass http://127.0.0.1;

  42. #}


  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  44. #

  45. #location ~ \.php$ {

  46. # root html;

  47. # fastcgi_pass 127.0.0.1:9000;

  48. # fastcgi_index index.php;

  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

  50. # include fastcgi_params;

  51. #}


  52. # deny access to .htaccess files, if Apache's document root

  53. # concurs with nginx's one

  54. #

  55. #location ~ /\.ht {

  56. # deny all;

  57. #}

  58. }



  59. # another virtual host using mix of IP-, name-, and port-based configuration

  60. #

  61. #server {

  62. # listen 8000;

  63. # listen somename:8080;

  64. # server_name somename alias another.alias;


  65. # location / {

  66. # root html;

  67. # index index.html index.htm;

  68. # }

  69. #}



  70. # HTTPS server

  71. #

  72. #server {

  73. # listen 443 ssl;

  74. # server_name localhost;


  75. # ssl_certificate cert.pem;

  76. # ssl_certificate_key cert.key;


  77. # ssl_session_cache shared:SSL:1m;

  78. # ssl_session_timeout 5m;


  79. # ssl_ciphers HIGH:!aNULL:!MD5;

  80. # ssl_prefer_server_ciphers on;


  81. # location / {

  82. # root html;

  83. # index index.html index.htm;

  84. # }

  85. #}

  86. }

2.Nginx虚拟主机配置

1.基于域名配置(使用的比较多)

1) 在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

  1. 1 # 基于域名的虚拟主机配置

  2. 2 server {

  3. 3 listen 80;

  4. 4 server_name www.xbq.com;

  5. 5 location / {

  6. 6 root html/host; # 相对路径,在 nginx/html/host目录中

  7. 7 index admin.html;

  8. 8 }

  9. 9 }

2) 在nginx/html文件夹中新建 host 文件夹,然后在 host文件夹中 新建admin.html文件,admin.html文件中的内容为:

Hello,This is host page,www.xbq.com.

3) 重新加载nginx.conf文件,./nginx -s reload

4) 修改C:\Windows\System32\drivers\etc\hosts文件,添加如下内容,为了将域名解析:

5) 浏览器访问:www.xbq.com,发现和刚刚写的admin.html内容一样,成功!

当访问www.xbq.com的时候,会匹配server中 server_name,然后找到html/host文件夹中的admin.html,返回界面。

2.基于端口配置

1) 在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

  1. 1 # 基于端口号的虚拟主机配置

  2. 2 server {

  3. 3 listen 8888;

  4. 4 server_name test; # 无实际意义,可省略

  5. 5 location / {

  6. 6 root html/port; # 相对路径,在nginx/html/port目录中

  7. 7 index admin.html;

  8. 8 }

  9. 9 }

2) 在nginx/html文件夹中新建 port文件夹,然后在 port文件夹中 新建admin.html文件,admin.html文件中的内容为:

Hello,This is port page.

3) 重新加载nginx.conf文件,./nginx -s reload

4) 浏览器访问:http://192.168.80.128:8888/,出现如下,则成功:

3.基于IP配置(使用的比较少)

(1) 先查看本机的IP,ifconfig

(2) 添加虚拟网卡

ifconfig eth0:1 192.168.80.150 broadcast 192.168.80.255 netmask 255.255.255.0 up

route add -host 192.168.80.150 dev eth0:1

(3) 检测网络是否通:ping 192.169.80.150

(4) 在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

  1. 1 # 基于IP的虚拟主机配置

  2. 2 server {

  3. 3 listen 80;

  4. 4 server_name 192.168.80.150; # 新建的虚拟网卡,是内网IP,只能通过 wget访问

  5. 5 location / {

  6. 6 root html/ip;

  7. 7 index admin.html;

  8. 8 }

  9. 9 }

(5) 在nginx/html文件夹中新建 ip文件夹,然后在 ip文件夹中 新建admin.html文件,admin.html文件中的内容为:

Hello,This is IP page.

(6) 重新加载nginx.conf文件,./nginx -s reload

(7) 到nginx/conf 目录下,访问 刚刚的IP地址:wget 192.168.80.150,发现下载成功!




相关文章