Welcome! Log In Create A New Profile

Advanced

I need help with VirtualHosts Please

Posted by pirulo 
I need help with VirtualHosts Please
September 21, 2010 04:14PM
Hello,

I need help setting up VirtualHosts in nginx please

Nginx was installed following this guide: http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/

I have a centos for testing internally before install on production server. the IP 192.168.1.106 is using for tests.

I wrote the settings for the domain www.prueba.com in nginx.conf file

I pointed www.prueba.com to 192.168.1.106 in / etc / hosts

marcelo @ inspiron: ~ $ ping www.prueba.com
PING www.prueba.com (192.168.1.106) 56 (84) bytes of data.


But when I open the browser to see www.prueba.com I get: http://img707.imageshack.us/img707/2840/pantallazoxm.png

My settings are:

[b]#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.codemongers.com/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################

#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.codemongers.com/NginxMainModule
#
#----------------------------------------------------------------------

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;



#----------------------------------------------------------------------
# Events Module
#
# http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------

events {
worker_connections 1024;
}


#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.codemongers.com/NginxHttpCoreModule
#
#----------------------------------------------------------------------

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

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

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

#
# The default server
#
server {
listen 80;
server_name _;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

}

#prueba virtual host

server {
listen 80;
server_name www.prueba.com;
root /var/www/virtuales/prueba.com;

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

server {
listen 80;
server_name www.othersite.com;
root /var/www/othersite.com/html;

location / {
index index.html index.htm;
}
}[/b]

Also I created /var/www/virtuales/prueba.com/index.html

Can you help me please?

Thanks .-
Re: I need help with VirtualHosts Please
September 22, 2010 06:09AM
I am assuming that when you go to www.prueba.com, it is showing the wrong index.html file right?

[quote=pirulo]
I wrote the settings for the domain www.prueba.com in nginx.conf file
[/quote]

You should place the server directive for the domain www.prueba.com inside the http directive. An example below:
[code]
http {
...
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
server_name www.prueba.com;
root /var/www/virtuales/prueba.com;

location / {
index index.html index.htm;
}
}
}
[/code]

A much more managable solution will be to place your virtual hosts inside this "/etc/nginx/conf.d/". As stated in your original configuration, you have asked nginx to include any other configuration files that is found in the above directory.

You can try the alternative solution which is to remove your virtual host configuration from nginx.conf, and put them inside a separate file for each domain. Then place those files inside "/etc/nginx/conf.d/".
Re: I need help with VirtualHosts Please
September 22, 2010 03:03PM
Thanks for the help, I really apreciate it!

The VirtualHosts configuration file is located in: /etc/nginx/conf.d/virtual.conf

example:

http {
### vhost1 - www.prueba.com ###
server {
listen 80;
server_name www.prueba.com;
access_log logs/prueba.access.log main;

index index.html;
root /var/www/virtuales/prueba.com/htdocs;
}



2010/09/22 13:21:22 [info] 24429#0: the configuration file /etc/nginx/nginx.conf syntax is ok
2010/09/22 13:21:22 [emerg] 24429#0: open() "/usr/share/nginx/logs/prueba.access.log" failed (2: No such file or directory)
2010/09/22 13:21:22 [emerg] 24429#0: the configuration file /etc/nginx/nginx.conf test failed

¿the logs file must be created maually everytime I create a domain?

I remove the "access_log logs/prueba.access.log main;" line and works, but when I browse "www.prueba.com" I get nginx default html page, and when I browse "www.prueba.com/index.html" the index.html placed in /var/www/virtuales/prueba.com/htdocs/html is showed.

is possible show index.html directly when "prueba.com" or "www.prueba.com" is entered in the browser?

Maybe some redirection in the VirtualHost configuration file

what about the error logs files?

Thank you very much!
Re: I need help with VirtualHosts Please
September 22, 2010 04:14PM
First for the logs problem; I think you want to store the access logs for www.prueba.com in a log folder under the root directory right? For that you will have to create the logs folder first:
[code]
mkdir -p /var/www/virtuales/prueba.com/logs
[/code]

Then proceed onto modifying the virtual host configuration file to use the folder you have just created. As seen from your latest post, you are putting the virtual host configuration in a different file than the Nginx. You will have to modify "/etc/nginx/conf.d/virtual.conf" to look like this:

[code]
server {
listen 80;
server_name www.prueba.com;
access_log /var/www/virtuales/prueba.com/logs/prueba.access.log main;

index index.html;
root /var/www/virtuales/prueba.com/htdocs;
}
[/code]

You need not include the http directive inside the virtual host configuration.

Second for the browsing issue. That is because your browser caches the file it has previously loaded at that particular url. You will have to do a force refresh or delete your cache/history to refresh in order to see the latest (Ctrl+F5 should do the trick).

There is no need to use virtual configuration to point prueba.com or www.prueba.com to one another. This action should be done via your dns control panel or through your domain registar. Assuming you have set your dns in order, then the force refresh of your browser should do the trick.
Re: I need help with VirtualHosts Please
September 22, 2010 04:54PM
allentkw, thank you!

Is working with html and images, but only with "www".

prueba.com shows 404, prueba.com/index.html shows nginx default index.

And try with another browser.

For now, this test domain is pointed to the server from my PC, editing my /etc/hosts file:

SERVER_IP www.prueba.com prueba.com

And via ping both www and without www are responding correctly to the SERVER_IP

Thahk you very much for your help!



Edited 1 time(s). Last edit at 09/22/2010 05:00PM by pirulo.
Re: I need help with VirtualHosts Please
September 22, 2010 05:22PM
Sorry!

My bad, got a little messed up here. Please ignore:
[quote=allentkw]
There is no need to use virtual configuration to point prueba.com or www.prueba.com to one another. This action should be done via your dns control panel or through your domain registar. Assuming you have set your dns in order, then the force refresh of your browser should do the trick.
[/quote]

You can use multiple hostname for your server_name directive as shown in the:
[code]
http://wiki.nginx.org/NginxHttpCoreModule#server_name
[/code]

You can modify your virtual host configuration to look like this:
[code]
server {
listen 80;
server_name www.prueba.com prueba.com;
access_log /var/www/virtuales/prueba.com/logs/prueba.access.log main;

index index.html;
root /var/www/virtuales/prueba.com/htdocs;
}
[/code]

This should work as intended. =)
Re: I need help with VirtualHosts Please
September 22, 2010 05:25PM
Is ok now!

Thank you!
Re: I need help with VirtualHosts Please
September 22, 2010 05:26PM
[quote=pirulo]
Is ok now!

Thank you!
[/quote]

My pleasure.

Enjoy your day! =D



Edited 1 time(s). Last edit at 09/22/2010 05:27PM by allentkw.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 277
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready