Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Setting method of virtual host and specified access path in Nginx

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the setting method of virtual host and specified access path in Nginx". In daily operation, I believe that many people have doubts about the setting method of virtual host and designated access path in Nginx. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "setting method of virtual host and specified access path in Nginx". Next, please follow the editor to study!

The configuration of virtual hosts on nginx is basically similar to that on apache.

The following points need to be noted:

First, about the .htaccess configuration, that is, for static configuration, on nginx, you usually have to write in the configuration text of the virtual host, but I have also seen the use of include files to solve this problem, that is, the include .htaccess file on the virtual host configuration script, but it has not been tried.

Second, how do you plan to run php,fastcgi? I do not think that this method spread on the Internet is a good way, on the contrary, I think as an excellent reverse proxy server should give full play to its reverse proxy advantages, so please consider the way to implement php.

All right, let's get back to business.

If you look at the directory structure of nginx, you probably already know what to do, which is basically similar to the virtual host configuration of apache.

Create a new file on / etc/nginx/sites-available, for example, call it

And then

Vi

The contents of the file are as follows:

Server {listen [:]: 80th serveridentiname jb51.net;root / var/ http://www.bbqmw.net/qm_yeqm//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 should say a few more words, because you may think of server_alias, in fact, the first one in nginx is server_name, followed by server_alias, so there is no need to declare the server alias name alias in nginx, this apache has a big difference, note.

Index is the order in which web pages are found.

Include is an include file. What is the purpose of the included file? It specifies the operation mode of php, file cache and so on. I might as well post one of the configurations I prompted:

Location ~ *\. (ico | css | js | gif | jpe?g | png) (\? [0-9] +)? ${expires max;break;} location ~. *\ .php$ {# fastcgi_pass 127.0.0.1Fastcgified pass unix:/dev/shm/php-cgi.sock;fastcgi_index index.php;include / etc/nginx/fastcgi_params;} if ($fastcgi_script_name ~\. *\ /. * php) {return 403;}

In the end, location / nginx_status is quite similar to apache's server-status, so I won't say much.

Location / nginx_status {stub_status on;access_log off;allow all;}

Then the second step is to establish a soft connection to the sites-enable.

Ln-s / etc/nginx/sites-available/ / etc/nginx/sites-enabled/

Do you need to check that the configuration syntax is correct?

Check it out:

/ etc/init.d/nginx configtestTesting nginx configuration: nginx.

No error is returned, just restart nginx.

/ etc/init.d/nginx restart

Specify access path

Niginx doesn't seem to have the idea of a virtual directory, but you can specify the path that nginx accesses when you request a path, which is a solution.

Server {listen 80 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;}}

At this point, the study on "the setting method of virtual host and specified access path in Nginx" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report