let's say i have two domain names: one.com and two.com.

There is also a project in PHP that is hosted on the server (NGINX) and is available via the following links. http://my.server.com/ ** one * / and http://my.server.com/ ** two * /.

How can I make it so that when a client in the browser asked one.com , it showed content from http://my.server.com/ one /, and when two.com , it showed content from http: //my.server .com / two / while in the address of the browser continued to show one.com or two.com, respectively?

How to do this with nginx?

Thank you in advance.

#user nobody; worker_processes auto; worker_rlimit_nofile 2048; error_log /var/log/nginx/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 2048; } http { server_tokens off; include mime.types; default_type application/octet-stream; log_format main '$http_x_forwarded_for - $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; # Websocket support #upstream websocket { # server 127.0.0.1:<PORT>; #} server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; include /etc/nginx/aliases.conf; #location /ws { # proxy_pass http://websocket; # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection "Upgrade"; #} location / { root /var/www/webroot/ROOT; index index.html index.htm index.php; location ~ \.php$ { location ~ /\. { deny all; access_log off; log_not_found off; } include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param PATH_INFO $fastcgi_script_name; } } index index.php index.html index.htm; #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; #} location ~ /\. { deny all; access_log off; log_not_found off; } # 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; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} location ~ \.php$ { location ~ /\. { deny all; access_log off; log_not_found off; } include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param DOCUMENT_ROOT /var/www/webroot; } } include /etc/nginx/conf.d/*.conf; } 

  • Attach to the text of the question, please, your server {...} section server {...} , so that you do not need to describe all possible configuration options. - aleksandr barakin
  • added a response based on the file you attached. see the paragraph after the word update . - aleksandr barakin

1 answer 1

First of all, the dns-names one.com and two.com should be resolved to the ip-address of your server.

secondly, you need to add two server {...} sections to the nginx configuration.

  1. Let's say you now have about this section (only relevant information is shown):

     server { server_name my.server.com; ... root /путь/к/каталогу; ... } 

    this is the case if the site code is in the directories /путь/к/каталогу/one and /путь/к/каталогу/two .

    you need to double-copy this section and correct the server_name and root directives to get something like the following:

     server { server_name my.server.com; ... root /путь/к/каталогу; ... } server { server_name one.com; ... root /путь/к/каталогу/one; ... } server { server_name two.com; ... root /путь/к/каталогу/two; ... } 

    update : the root directive can also be inside the location / {...} section, as in your question. this does not change the essence - copy the server {...} section "as is", without changing the location of the root directive (i.e., leaving it inside the location / {...} section), but be sure to add /one and /two at the end of the root directives, as in the example above.

  2. if the one and two directories are in completely different places, then the section probably looks something like this:

     server { server_name my.server.com; ... location /one { root /один/путь; ... } location /two { root /другой/путь; ... } ... } 

    then again you need to make two copies of the server section, deleting unnecessary sections location {...} :

     server { server_name my.server.com; ... } server { server_name one.com; ... location /one { root /один/путь; ... } ... } server { server_name two.com; ... location /two { root /другой/путь; ... } ... } 

    and then correcting /one and /two on / , and add these paths to the root directives:

     server { server_name my.server.com; ... } server { server_name one.com; ... location / { root /один/путь/one; ... } ... } server { server_name two.com; ... location / { root /другой/путь/two; ... } ... } 
  • "First, the dns names one.com and two.com should be resolved to the ip address of your server." - Thanks for the answer, but can we give a little more detail on this? - user1167253
  • what kind of information interests you? What is dns in general? - aleksandr barakin
  • "The dns names one.com and two.com should be resolved to the ip address of your server." - What does it mean to "rezolvitsya"? how to do it? - user1167253
  • @ user1167253, “rekolvitsya” (from the English resolve) - this means that the dns-request of the form “get an a-record for the domain name of such and such” returns an a-record containing the required ip-address. further "educational program", I'm sorry, is inappropriate here (comments are not intended for this). If you have a new question - ask it using the "Ask a Question" button in the upper right corner of the page. - aleksandr barakin