Nginx virtual host and specified access path Settings

  • 2020-05-10 23:29:13
  • OfStack

Add multiple virtual hosts

Recently, the installation of nginx on ubuntu was successful, but rewrite was the only one that didn't try, because there were multiple websites on the server, so I didn't dare to try it on the server. Take your time. I looked up some articles on the Internet, and I left one for experiment.
The configuration of the virtual host on nginx is essentially the same as that on apache.
A few points to note:
1. About the.htaccess configuration, which is for static configuration, on nginx 1 you usually have to write in the configuration text of the virtual host, but I have also seen the inclusion file used to solve this problem, that is, the include.htaccess file on the virtual host configuration script, but I haven't tried it yet.
2. How do you plan to run php and fastcgi? I don't think this method circulating on the Internet is a good one. On the contrary, I think as a good reverse proxy server, it should take advantage of its reverse proxy, so please consider the way to implement php first.
All right, back to business.
If you look at the directory structure of nginx, you probably already know what to do, which is similar to the virtual host configuration of apache.
Create a new file on /etc/nginx/ sites-available, for example, www.ofstack.com
then


vi www.ofstack.com

Contents of the added document are as follows:


server
{
listen [::]:80;
server_name www.ofstack.com ofstack.com;
root /var/www/ofstack.com;
index index.html index.htm index.php;
include /etc/nginx/common.conf;
location /nginx_status
{
stub_status on;
access_log off;
allow all;
}
}

Simple explanation 1:
listen is for listening port, needless to say;
server_name say a few more words, because you might think of server_alias, actually in nginx the first is server_name, server_alias, so in nginx server alias name alias is not required to declare, this is a big difference apache, notice.
index is the order in which web pages are searched
include contains files. www.ofstack.com contains files for what purpose? Inside is the operation mode of php specified, file cache, etc., I might as well put up the configuration I suggested:


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 is equivalent to server-status of apache, not much to say.


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

Then step 2, create a soft connection to sites-enable


ln -s /etc/nginx/sites-available/www.ofstack.com /etc/nginx/sites-enabled/www.ofstack.com

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


/etc/init.d/nginx configtest

Testing nginx configuration: nginx.

No error was returned, just restart nginx.
/etc/init.d/nginx restart


Specify access path

niginx seems to have no such thing as a virtual directory, but you can specify the path to which nginx accesses when requesting a path, which is also a workaround.


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;
}
}

Note that location /phpadmin/ {} is completely different from location /phpadmin {}.

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

The following configuration is similar to the one above except that all access to /phpadmin/ will be resolved correctly and all other access will return information that does not (404) exist on the page.


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



Related articles: