nginx配置多端口多域名访问
2014-01-13 19:29
393 查看
linux 配置nginx多端口多域名
调了半年终于搞定,也不知道是什么问题,干!!
最后发现原因:(是.conf配件文件的格式不正确,win和linux有区别啊,安全起见还是拷贝原conf文件来改。)
1、先配置防火墙
有2种方法,一种是下面这样直接命令行敲,还有是修改etc下面的文件,最后记住保存一下,重启下防火墙。
开启防火墙:
直接通过IP和端口号也是可以直接访问的,只不过绑定了不同的域名。
这里我做了3个测试网址3、配置nginx.conf打开配置文件,路径:vi /usr/local/nginx/conf/nginx.conf其实这里没什么东东,看他这个块server{#一堆code,就是配置内容}include vhost/*.conf;#加上这句话,意思是包含vhost/文件夹下所有.conf文件,相当于多个server{}块包含进来吧,分开写更清晰些新建vhost目录,并新建test80.conf,test1010.conf两个配置文件,这2个文件内容其实意思差不多,1个是精简版本,1个是完整版本,具体根据需要
其实上面这种写法不好,在配置PHP时会出错,所以推荐的做法是把外面的大括号去除,即:
localtion/{}删除
server {
listen 80;
server_name test80.com;
root /home/wwwroot/default/demo;
index index.html index.htm;
}
正确配置文件在本文最后
正确配置文件:
b.1015.com.conf
server
{
listen 1015;
server_name b.1015.com;
index index.html index.htm index.php;
root /home/wwwroot/default/b;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
nginx.conf
user www www;
worker_processes 1;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_zone crawler $binary_remote_addr 10m;
server_tokens off;
#log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
listen 80;
server_name www.lnmp.org;
index index.html index.htm index.php;
root /home/wwwroot/default;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
include vhost/*.conf;
}
OK,都弄好了,最后重启下lngx即可。我这是一键包,直接/root/lnmp restart搞定。
预览下
http://test80.com/ http://test80.com:1010/
访问成功!结束!
附一些相关文章
防火墙开启端口
http://www.blogjava.net/Alpha/archive/2012/09/13/387640.html http://www.myhack58.com/Article/48/66/2012/34999.htm
nginx多站点配置
http://highsea90.com/index.php/archives/899
调了半年终于搞定,也不知道是什么问题,干!!
最后发现原因:(是.conf配件文件的格式不正确,win和linux有区别啊,安全起见还是拷贝原conf文件来改。)
1、先配置防火墙
有2种方法,一种是下面这样直接命令行敲,还有是修改etc下面的文件,最后记住保存一下,重启下防火墙。
开启防火墙:
/sbin/iptables -I INPUT -p tcp --dport 1010 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 1015 -j ACCEPT #保存 /etc/rc.d/init.d/iptables save #重启 /etc/init.d/iptables restart
#查看
vi /etc/sysconfig/iptables2、配置域名hots,其实就是域名解析,在本地的话直接修改hots文件如:#192.168.0.38 demo38.com192.168.0.26 test80.com192.168.0.26 test1010.com192.168.0.26 app.test1010.com注意,这里绑定不要加端口号,其实还是绑定的原IP,这个测试规则太垃圾!!
直接通过IP和端口号也是可以直接访问的,只不过绑定了不同的域名。
这里我做了3个测试网址3、配置nginx.conf打开配置文件,路径:vi /usr/local/nginx/conf/nginx.conf其实这里没什么东东,看他这个块server{#一堆code,就是配置内容}include vhost/*.conf;#加上这句话,意思是包含vhost/文件夹下所有.conf文件,相当于多个server{}块包含进来吧,分开写更清晰些新建vhost目录,并新建test80.conf,test1010.conf两个配置文件,这2个文件内容其实意思差不多,1个是精简版本,1个是完整版本,具体根据需要
#test80.conf文件内容: server { listen 80; server_name test80.com; location / { root /home/wwwroot/default/demo; index index.html index.htm; } }
其实上面这种写法不好,在配置PHP时会出错,所以推荐的做法是把外面的大括号去除,即:
localtion/{}删除
server {
listen 80;
server_name test80.com;
root /home/wwwroot/default/demo;
index index.html index.htm;
}
正确配置文件在本文最后
#test1010.conf内容: server { listen 1010; server_name test1010.com; index index.html index.htm index.php; root /home/wwwroot/default/demo; #charset koi8-r; #access_log logs/host.access.log main; #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 html; } # 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; #include fastcgi_params; include fastcgi.conf; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
server { listen 1010; server_name app.test1010.com; index index.html index.htm index.php; root /home/wwwroot/default2; #charset koi8-r; #access_log logs/host.access.log main; #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 html; } # 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; #include fastcgi_params; include fastcgi.conf; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
正确配置文件:
b.1015.com.conf
server
{
listen 1015;
server_name b.1015.com;
index index.html index.htm index.php;
root /home/wwwroot/default/b;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
nginx.conf
user www www;
worker_processes 1;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_zone crawler $binary_remote_addr 10m;
server_tokens off;
#log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
listen 80;
server_name www.lnmp.org;
index index.html index.htm index.php;
root /home/wwwroot/default;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
include vhost/*.conf;
}
OK,都弄好了,最后重启下lngx即可。我这是一键包,直接/root/lnmp restart搞定。
预览下
http://test80.com/ http://test80.com:1010/
访问成功!结束!
附一些相关文章
防火墙开启端口
http://www.blogjava.net/Alpha/archive/2012/09/13/387640.html http://www.myhack58.com/Article/48/66/2012/34999.htm
nginx多站点配置
http://highsea90.com/index.php/archives/899
相关文章推荐
- nginx超时设置
- keepalived做nginx的高可用,企业版简单介绍。
- nginx 工作模型
- 搭建linux+nginx+mysql+php环境
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]
- Linux下安装Nginx
- 实例讲解Nginx下的rewrite规则
- Nginx开启Gzip压缩大幅提高页面加载速度及相关测试
- select与epoll、apache与nginx实现原理对比
- CentOS6.5安装nginx1.5.8时出现“cp: "conf/koi-win" 与"/usr/local/nginx/conf/koi-win" 为同一文件”的解决方法
- Nginx安装及配置文件详细说明
- Nginx负载均衡配置
- CentOS5安装Nginx1.4+PHP5.5 FastCGI
- Nginx+Naxsi部署专业级Web应用防火墙
- #centOS 安装nginx
- Nginx+Naxsi部署专业级web应用防火墙
- 姚博文 nginx安装
- Nginx + PHP Web服务器
- nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器
- nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器