博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx虚拟主机配置
阅读量:6907 次
发布时间:2019-06-27

本文共 5698 字,大约阅读时间需要 18 分钟。

新版本nginx的配置文件被拆分为若干部分

1、主配置文件为nginx.conf

2、与php相关的是fastcgi_params

3、与python相关的是uwsgi_params

4、...其他配置文件

[PS:首先确保占用80端口的服务被停止,nginx默认监听端口为80]

我们首先可以打开nginx.conf

 nginx.conf主配置文件
#使用的用户和组#user nobody;#指定工作衍生进程数(默认为CPU的线程数!)worker_processes 2;#指定pid存放的路径,应该记录了nginx守护进程的进程号,是其他进程的父进程id!(如Apache下有PidFile )pid /var/run/nginx.pid;events {use epoll;#linux下性能最好的event模式!#允许的连接数worker_connections 768;# multi_accept on;}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;#keepalive_timeout 测试多次请求传输之间的时间,再一次持久链接,如果客户端的两次HTTP请求超过keepalive_timeout的时间,则关闭链接释放资源!!(Apache下也有)keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;#default_type application/octet-stream;#当用户请求的文件木有在服务器中定义mime类型映射,使用DefaultType#可以有啥? text/html text/plain(表示后缀为txt的文件以文本显示) image/gif#这里的application/octet-stream 是啥?#若未定的文件多是软件或者图像,建议为application/octet-stream#浏览器会提示用户以“另存为”的方式保存文件!### Logging Settings###日志,详细见后面access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### Gzip Settings##开启gzip压缩gzip on;gzip_disable "msie6";# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;### Virtual Host Configs##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;##很好,我们可以清楚的看到,我们会加载conf.d目录与sites-enabled目录下得所有文件,这也证明了我们为啥子可以在这两个目录下写配置文件!}#nginx同样可以做邮件代理服务器!#mail {# # See sample authentication script at:# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript## # auth_http localhost/auth.php;# # pop3_capabilities "TOP" "USER";# # imap_capabilities "IMAP4rev1" "UIDPLUS";## server {# listen localhost:110;# protocol pop3;# proxy on;# }## server {# listen localhost:143;# protocol imap;# proxy on;# }#}

 nginx的虚拟主机配置

如下图是最简单的虚拟主机配置文件

 

与Apache相同,nginx也可以配置多种类型的虚拟主机。

  • 基于域名的虚拟主机
  • 基于IP的虚拟主机
  • 基于端口的虚拟主机

然后我们发现目录下有site-available与site-enabled两个目录,和Apache一模一样,我们一般采用的方法是在前者下写好配置文件,到后者目录下做好一个软连接!原因如同目录的名字一样,前者是存在的网站,而后者是正在使用的目录!nginx默认会加载site-enabled目录!前者的目录下有一个default给我们参考如何写虚拟主机的配置文件

让我们来看一下这段:

# You may add here your# server {# ...# }# statements for each of your virtual hosts to this file#这段文字很清晰告诉我们如何添加虚拟主机!### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.####上面这段讲了具体可以参考那些网站和例子!server {#开始了直接server打头,如果配置在nginx.conf里,请写在http{……server{……}}里,咱们是基于http协议的!#listen 80; ## listen for ipv4; this line is default and implied#监听80端口!#listen [::]:80 default ipv6only=on; ## listen for ipv6#root HTML网页文件存放的目录root /usr/share/nginx/www;#默认首页文件,顺序从左往右!如果找不到index.html,就去找index.htm`index index.html index.htm;# Make site accessible from http://localhost/server_name localhost;#主机名称location / {# First attempt to serve request as file, then# as directory, then fall back to index.htmltry_files $uri $uri/ /index.html;}#显而易见,访问根目录,直接跳转到首页!location /doc {root /usr/share;autoindex on;#allow 127.0.0.1;deny all;}location /images {root /usr/share;autoindex off;}#这里涉及了404,50x的页面及其地址#error_page 404 /404.html;# redirect server error pages to the static page /50x.html##error_page 500 502 503 504 /50x.html;#location = /50x.html {# root /usr/share/nginx/www;#}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# 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;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# root html;# index index.html index.htm;## location / {# try_files $uri $uri/ /index.html;# }#}# HTTPS server##server {# listen 443;# server_name localhost;## root html;# index index.html index.htm;## ssl on;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;## ssl_session_timeout 5m;## ssl_protocols SSLv3 TLSv1;# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;# ssl_prefer_server_ciphers on;## location / {# try_files $uri $uri/ /index.html;# }#}

我们可以发现要配多个虚拟主机基本的格式是

server{……}

server{……}

虚拟主机建立的方式共分为三种:基于IP的虚拟主机,基于端口的虚拟主机和基于名称的虚拟主机。前两种由于受到成本和客户使用习惯的限制,相对使用的没有基于名称的虚拟主机多。

1、基于主机名称的虚拟机配置server{ listen 80; server_name www.example.com} server{
listen 80;
server_name www.test.com} 
server{ listen 80 default_server;  ...}

  上述配置中, 定义了三个虚拟主机。前两个 server, 通过域名“www.example.com” 和 “www.test.com” 可以分别访问正确的网站。如果浏览器直接通过 IP 地址或者其他指向这台机器的域名访问, 那么访问到的是第三个 server 配置。第三个 server 为一个默认配置, 请注意它没有“server_name”指令, 并且“listen”指令包含一个“default_server”关键字。

2、基于IP的虚拟主机 server{  listen 10.0.0.88:80; root 88.com;  index index.html; } server{ listen 10.0.0.87:80; root 87.com; index index.html; }

[PS:请自行分配相应IP地址,并建立88.com与87.com目录]  

以上配置了两台虚拟主机,一台 IP 为 10.0.0.88,另一台为 10.0.0.87。它们都监听 80端口。根据访问的 IP 地址不同,返回不同网站内容。

3、基于端口的虚拟主机server{ listen 80; root 80.com; }server{ listen 8080; root 8080.com;}

以上配置了两台虚拟主机,一台使用相同 IP。一台使用 80 端口,另一台使用 8080 端口。访问 8080 端口时需要在 URL 后加上 :8080 。

转载于:https://www.cnblogs.com/cugb-2013/p/3642674.html

你可能感兴趣的文章
初探 ELK - 每天5分钟玩转 Docker 容器技术(89)
查看>>
c#通过创建Windows服务启动程序
查看>>
系统架构设计指南
查看>>
我的友情链接
查看>>
Jquery Ajax方法传值到action
查看>>
亚马逊图书推荐--我感兴趣的
查看>>
Xmanager连接Centos6.3的远程桌面
查看>>
Office365:客户端升级后无法启动Microsoft Outlook
查看>>
我的友情链接
查看>>
在eclipse中查看Android源代码
查看>>
prometheus+grafana
查看>>
Liferay 启动过程分析3-处理启动事件(第四部分)
查看>>
Rust语言开发基础(七)Rust 特性
查看>>
CountDownLatch示例
查看>>
Windows 8 相关资源 MSDN原版
查看>>
NetScaler VPX 10实施1:NetScaler入门
查看>>
如何优化eclipse
查看>>
互联互通网络质量分析
查看>>
记一次OOM排查解决
查看>>
AIX 6.1 + HACMP 6.1 + Oracle 11g双机实施 (3) --- AIX 6.1 Cloning an Oracle Home
查看>>