您的位置:首页 > 运维架构 > Nginx

nginx配置多端口多域名访问

2014-01-13 19:29 393 查看
linux 配置nginx多端口多域名

调了半年终于搞定,也不知道是什么问题,干!!

最后发现原因:(是.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/iptables
2、配置域名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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: