How to set up a virtual host and a specified access path in Nginx _nginx

Source: Internet
Author: User
Tags nginx server

Add multiple virtual Hosts

Recently in Ubuntu churn Nginx, the installation was successful, only rewrite did not test, because the server has more than one website, still dare not try on the server, slowly. On the Internet to check some articles, the next one left to do the experiment.
The configuration of the virtual host on Nginx is essentially similar to that of Apache.
The points to be noted are:
First, about the. htaccess configuration, that is, static configuration, in the Nginx generally you want to write in the configuration text of the virtual host, but I also see the inclusion file to solve this problem, that is, in the virtual host configuration script include the. htaccess file, but no no try.
Second, how to plan how to run php,fastcgi? I don't think it's a good idea to circulate on the internet, but instead I think that as an excellent reverse proxy server should play the advantage of its reverse proxy, so the way to execute PHP, please consider it first.
All right, back to the point.
Looking at the Nginx directory structure, you probably already know what to do with Apache's virtual host configuration.
Create a new file on the/etc/nginx/sites-available, like call www.jb51.net.
And then

VI www.jb51.net

The contents of the accession document are as follows:

Server
{
Listen [::]:80;
server_name www.jb51.net jb51.net;
root/var/www/jb51.net;
Index index.html index.htm index.php;
include/etc/nginx/common.conf;
Location/nginx_status
{
stub_status on;
Access_log off;
Allow all;
}

A simple explanation:
Listen is the listening port, needless to say;
server_name to say a few more, because you may think of the server_alias, in fact, the first in Nginx is server_name, the back is Server_alias, so in the Nginx server alias The name alias is not to be declared separately, this Apache has a big difference, notice.
Index is the order in which pages are searched
Include is the include file, what is the file that www.jb51.net contains? Inside is to specify the running mode of PHP, file caching, etc., I might as well put the configuration of my hint to come up:

Location ~* \. (ico|css|js|gif|jpe?g|png) (\? [0-9]+)? $ {
expires Max;
break;
Location ~ *\.php$ {
# fastcgi_pass 127.0.0.1:9000;
Fastcgi_pass Unix:/dev/shm/php-cgi.sock;
Fastcgi_index index.php;
include/etc/nginx/fastcgi_params;
}
if ($fastcgi _script_name ~ \ ... *\/.*php) {return
403;
}

Finally location/nginx_status quite with Apache Server-status, not much said.

Location/nginx_status
{
stub_status on;
Access_log off;
Allow all;
}

And then step two, build a soft connection to the sites-enable inside.

Ln-s/etc/nginx/sites-available/www.jb51.net/etc/nginx/sites-enabled/www.jb51.net

Do you need to check to see if the configuration syntax is correct?
Check:

/etc/init.d/nginx configtest

testing Nginx Configuration:nginx.

No return error, restart Nginx is OK.
/etc/init.d/nginx restart


Specify Access Path

Niginx does not seem to have a virtual directory, but it can be a solution to specify the path to Nginx access when requesting a path.

server {
listen    default;
server_name _;

Location/{
root  html;
Index 403.html;
}

Location ~//.ht {
deny all;
}

  location/phpadmin/{
alias  /opt/www/phpadmin/;
Index  index.php;
}
Location ~/.php$ {
include httpd.conf;
}
}

Note that location/phpadmin/{} and Location/phpadmin {} are completely different.

The former can access the directory, and the latter will be redirected to the server, such as: Http://127.0.0.1/phpadmin, which will be redirected to Http://_/phpadmin

The following configuration is basically similar to the above, except that all access to/phpadmin/is parsed correctly, while other accesses return information that does not exist on the page (404).

server {
listen    default;
server_name _;

Location/{
root  html;
#index 403.html;

return 404;
}

Location ~//.ht {
deny all;
}

  location/phpadmin/{
alias  /opt/www/phpadmin/;
Index  index.php;
}
Location ~/.php$ {
include httpd.conf;
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.