Ubuntu Linux nginx 虚拟主机配置 和伪静态Rewrite 配置笔记

拷贝一份/etc/nginx/sites-available下的default

# You may add here your
# server {
# …
# }
# statements for each of your virtual hosts

server {
listen 80 default;
server_name localhost;

access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default;
index index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}

#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 /var/www/nginx-default;
#}

# 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;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#includefastcgi_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;

#location / {
#root html;
#index index.html index.htm;
#}
#}

# HTTPS server
#
#server {
#listen 443;
#server_name localhost;

#ssl on;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;

#ssl_session_timeout 5m;

#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;

#location / {
#root html;
#index index.html index.htm;
#}
#}
然后改名名字比如home
修改特别是红色部分
server {
# Replace this port with the right one for your requirements
listen 80;

# Multiple hostnames separated by spaces. Replace these as well.
server_name home.forerx.cn; #Alternately: _

root /var/www/home;

error_page 404 /404.html;
access_log /var/log/nginx/nginx_home.log;

index index.php index.html index.htm;

# serve static files directly
location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires 30d;
}

#以下是Rewrite 规则
rewrite ^(.*)/(space|network)\-(.+)\.html$ $1/$2.php?rewrite=$3 last;
rewrite ^(.*)/(space|network)\.html$ $1/$2.php last;
rewrite ^(.*)/([0-9]+)$ $1/space.php?uid=$2 last;
#以上是Rewrite 规则 要注意直接放在没括号的位置就行

location ~ .php$ {
# By all means use a different server for the fcgi processes if you need to
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
}

location ~ /\.ht {
deny all;
}

启用虚拟主机ln -s /etc/nginx/sites-available/home /etc/nginx/sites-enabled/home
会在/etc/nginx/sites-enabled下生成同名文件home跟apache有点像,但命令不一样
同样按照上面办法可以配置出无限站点来,只要主机配置强大!
然后重启nginx服务
sudo /etc/init.d/nginx restart
php-cgi启动(没它会报502,稍后搞定它自动启动)spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

投稿作者 作者网站

评论

 
 

为您推荐


请支持IMCN发展!

谁在捐赠

微信捐赠 支付宝捐赠
微信捐赠 支付宝捐赠
ta的个人站点

发表文章4162篇

关注我的头条 不要放弃,百折不挠,坚强、自信。


微信公众号二维码

归档