问题:nginx如何多个域名绑定一个项目,一个端口,但是子目录。
解决:做www.xiao.com项目时,需要做移动端,m.xiao.com,但不想再开服务,所以绑定的ip和端口就是一个
方法:
1、www.xiao.com 的nginx配置文件如下
upstream xiao {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name xiao.com www.xiao.com;
server_tokens off;
if ($host != 'www.xiao.com'){
rewrite ^/(.*)$ http://www.xiao.com/$1 permanent;
}
access_log /alidata/log/nginx/access/dongman.log;
error_log /alidata/www/dongman/website/logs/error.log;
client_max_body_size 50m;
location /static/ {
root /alidata/www/dongman/website/;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://xiao;
# track_uploads proxied 30s;
}
}
2、m.xiao.com 的nginx的配置如下
upstream mxiao {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name m.xiao.com;
server_tokens off;
access_log /alidata/log/nginx/access/dongman.log;
error_log /alidata/www/dongman/website/logs/error.log;
client_max_body_size 50m;
location /static/ {
root /alidata/www/dongman/website/;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://mxiao/m/;
# track_uploads proxied 30s;
}
}
注:tornado作为服务器
如果使用nginx作为服务器时,只需要更改运行的路径就行了,例如m.man.com
server {
listen 80;
server_name m.man.com;
index index.html index.htm index.php;
root /alidata/www/man/m;
if ($host != 'm.man.com') {
rewrite ^/(.*)$ http://m.man.com/$1 permanent;
}
location ~ .*.(php|php5)?$ {
# fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*.(js|css)?$ {
expires 1h;
}
access_log /alidata/log/nginx/access/m.man.log;
error_page 404 /404.html;
}
这是php文件,所以指定运行目录就能运行了