如何使用nginx为多个web服务器提供服务



我已经安装了nginx,我想在同一服务器上的同一用户下提供两个不同的web应用程序。

这是我已经使用的配置:

server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://www.example.com$request_uri;
}
# HTTPS — proxy all requests to the Node app
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
location ~* .(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Vary Accept-Encoding;
access_log off;
}
root /home/myuser/main/dist;
# Use the Let’s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
}

正如你所看到的,我在/home/myuser目录下有一个名为main的目录和一个dist目录。

我想在myuser目录下添加另一个目录,例如test。

因此,我将使用/myuser/test并在那里为另一个web应用程序提供服务器。使用相同的nginx服务器。

我曾尝试在上面提到的配置文件中编写许多变体,但都无法运行。

配置文件位于:/etc/nginx/sites-enabled/example.com.conf

我用sudo编辑它。

如果您想从本地目录托管不同的静态文件,配置可能如下所示:

注意:如果使用root指令,您的位置uri(/,/one(将被附加到根目录路径。

root指令可以在每个位置块中用于设置文档根。http://nginx.org/en/docs/http/ngx_http_core_module.html#root

这就是alias存在的原因。使用alias时,该位置不会成为目录路径的一部分。看看这个:http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

1.一个域-多个位置

server {

server_name example.com;
listen 443 ssl;
.....
root /home/user/main/dist;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
location /two {
alias /home/main/example;
index index.html;
..... 
}

}

2.两个域-单个位置

server {

server_name example.com;
listen 443 ssl;
.....
root /home/user/main/dist;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
}
server {

server_name example1.com;
listen 443 ssl;
.....
root /home/user/main/test;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
}

最新更新