nginx配置虚拟主机

原来的代码如下:

View Code
  1 # You may add here your
  2 # server {
  3 #    ...
  4 # }
  5 # statements for each of your virtual hosts to this file
  6 
  7 ##
  8 # You should look at the following URL's in order to grasp a solid understanding
  9 # of Nginx configuration files in order to fully unleash the power of Nginx.
 10 # http://wiki.nginx.org/Pitfalls
 11 # http://wiki.nginx.org/QuickStart
 12 # http://wiki.nginx.org/Configuration
 13 #
 14 # Generally, you will want to move this file somewhere, and start with a clean
 15 # file but keep this around for reference. Or just disable in sites-enabled.
 16 #
 17 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
 18 ##
 19 
 20 server {
 21     #listen   80; ## listen for ipv4; this line is default and implied
 22     #listen   [::]:80 default ipv6only=on; ## listen for ipv6
 23 
 24     # root /usr/share/nginx/www;
 25     root /var/www;
 26     index index.html index.htm index.php;
 27 
 28     # Make site accessible from http://localhost/
 29     server_name localhost;
 30 
 31     location / {
 32         # First attempt to serve request as file, then
 33         # as directory, then fall back to index.html
 34         try_files $uri $uri/ /index.html;
 35         # Uncomment to enable naxsi on this location
 36         # include /etc/nginx/naxsi.rules
 37     }
 38 
 39     location /doc/ {
 40         alias /usr/share/doc/;
 41         autoindex on;
 42         allow 127.0.0.1;
 43         deny all;
 44     }
 45 
 46     # Only for nginx-naxsi : process denied requests
 47     #location /RequestDenied {
 48         # For example, return an error code
 49         #return 418;
 50     #}
 51 
 52     #error_page 404 /404.html;
 53 
 54     # redirect server error pages to the static page /50x.html
 55     #
 56     #error_page 500 502 503 504 /50x.html;
 57     #location = /50x.html {
 58     #    root /usr/share/nginx/www;
 59     #}
 60 
 61     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 62     #
 63     location ~ \.php$ {
 64     #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
 65     #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 66     #
 67     #    # With php5-cgi alone:
 68         fastcgi_pass 127.0.0.1:9000;
 69     #    # With php5-fpm:
 70     #    fastcgi_pass unix:/var/run/php5-fpm.sock;
 71         fastcgi_index index.php;
 72         include fastcgi_params;
 73     }
 74 
 75     # deny access to .htaccess files, if Apache's document root
 76     # concurs with nginx's one
 77     #
 78     #location ~ /\.ht {
 79     #    deny all;
 80     #}
 81 }
 82 
 83 # another virtual host using mix of IP-, name-, and port-based configuration
 84 #
 85 #server {
 86 #    listen 8000;
 87 #    listen somename:8080;
 88 #    server_name somename alias another.alias;
 89 #    root html;
 90 #    index index.html index.htm;
 91 #
 92 #    location / {
 93 #        try_files $uri $uri/ /index.html;
 94 #    }
 95 #}
 96 
 97 
 98 # HTTPS server
 99 #
100 #server {
101 #    listen 443;
102 #    server_name localhost;
103 #
104 #    root html;
105 #    index index.html index.htm;
106 #
107 #    ssl on;
108 #    ssl_certificate cert.pem;
109 #    ssl_certificate_key cert.key;
110 #
111 #    ssl_session_timeout 5m;
112 #
113 #    ssl_protocols SSLv3 TLSv1;
114 #    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
115 #    ssl_prefer_server_ciphers on;
116 #
117 #    location / {
118 #        try_files $uri $uri/ /index.html;
119 #    }
120 #}

配置好后的default文件是这样的

View Code
  1 # You may add here your
  2 # server {
  3 #    ...
  4 # }
  5 # statements for each of your virtual hosts to this file
  6 
  7 ##
  8 # You should look at the following URL's in order to grasp a solid understanding
  9 # of Nginx configuration files in order to fully unleash the power of Nginx.
 10 # http://wiki.nginx.org/Pitfalls
 11 # http://wiki.nginx.org/QuickStart
 12 # http://wiki.nginx.org/Configuration
 13 #
 14 # Generally, you will want to move this file somewhere, and start with a clean
 15 # file but keep this around for reference. Or just disable in sites-enabled.
 16 #
 17 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
 18 ##
 19 
 20 server {
 21     #listen   80; ## listen for ipv4; this line is default and implied
 22     #listen   [::]:80 default ipv6only=on; ## listen for ipv6
 23 
 24     # root /usr/share/nginx/www;
 25     root /var/www;
 26     index index.html index.htm index.php;
 27 
 28     # Make site accessible from http://localhost/
 29     server_name localhost;
 30 
 31     location / {
 32         # First attempt to serve request as file, then
 33         # as directory, then fall back to index.html
 34         try_files $uri $uri/ /index.html;
 35         # Uncomment to enable naxsi on this location
 36         # include /etc/nginx/naxsi.rules
 37     }
 38 
 39     location /doc/ {
 40         alias /usr/share/doc/;
 41         autoindex on;
 42         allow 127.0.0.1;
 43         deny all;
 44     }
 45 
 46     # Only for nginx-naxsi : process denied requests
 47     #location /RequestDenied {
 48         # For example, return an error code
 49         #return 418;
 50     #}
 51 
 52     #error_page 404 /404.html;
 53 
 54     # redirect server error pages to the static page /50x.html
 55     #
 56     #error_page 500 502 503 504 /50x.html;
 57     #location = /50x.html {
 58     #    root /usr/share/nginx/www;
 59     #}
 60 
 61     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 62     #
 63     location ~ \.php$ {
 64     #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
 65     #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 66     #
 67     #    # With php5-cgi alone:
 68         fastcgi_pass 127.0.0.1:9000;
 69     #    # With php5-fpm:
 70     #    fastcgi_pass unix:/var/run/php5-fpm.sock;
 71         fastcgi_index index.php;
 72         include fastcgi_params;
 73     }
 74 
 75     # deny access to .htaccess files, if Apache's document root
 76     # concurs with nginx's one
 77     #
 78     #location ~ /\.ht {
 79     #    deny all;
 80     #}
 81 }
 82 
 83 server {
 84     root /var/www/test;
 85     index index.html index.htm index.php;
 86 
 87     # Make site accessible from http://localhost/
 88     server_name www.xukun.com;
 89 
 90     location / {
 91         # First attempt to serve request as file, then
 92         # as directory, then fall back to index.html
 93         try_files $uri $uri/ /index.html;
 94         # Uncomment to enable naxsi on this location
 95         # include /etc/nginx/naxsi.rules
 96     }
 97 
 98     location /doc/ {
 99         alias /usr/share/doc/;
100         autoindex on;
101         allow 127.0.0.1;
102         deny all;
103     }
104     location ~ \.php$ {
105         fastcgi_pass 127.0.0.1:9000;
106         fastcgi_index index.php;
107         include fastcgi_params;
108     }
109 }
110 # another virtual host using mix of IP-, name-, and port-based configuration
111 #
112 #server {
113 #    listen 8000;
114 #    listen somename:8080;
115 #    server_name somename alias another.alias;
116 #    root html;
117 #    index index.html index.htm;
118 #
119 #    location / {
120 #        try_files $uri $uri/ /index.html;
121 #    }
122 #}
123 
124 
125 # HTTPS server
126 #
127 #server {
128 #    listen 443;
129 #    server_name localhost;
130 #
131 #    root html;
132 #    index index.html index.htm;
133 #
134 #    ssl on;
135 #    ssl_certificate cert.pem;
136 #    ssl_certificate_key cert.key;
137 #
138 #    ssl_session_timeout 5m;
139 #
140 #    ssl_protocols SSLv3 TLSv1;
141 #    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
142 #    ssl_prefer_server_ciphers on;
143 #
144 #    location / {
145 #        try_files $uri $uri/ /index.html;
146 #    }
147 #}


我修改的文件是/etc/nginx/sites-available/default

而很多人修改的是nginx.conf文件,不知道是不是和安装方式有关,我是使用apt-get install方式安装的

原文地址:https://www.cnblogs.com/quinnxu/p/3059695.html