1

我想通过路径中的第一项来服务我的项目,例如http://example.com/projectname应该服务于 /usr/share/nginx/html/projectname 中的项目。

这是我的配置的样子:

server {
    listen 80;
    server_name example.com www.example.com;
    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
    listen       443 ssl;
    listen       [::]:443 ssl;

    ssl_certificate "/etc/ssl/XX.pem";
    ssl_certificate_key "/etc/ssl/XX.key";
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    server_name  example.com/$1 www.example.com/$1;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;

    location /projectname {
        root   /usr/share/nginx/html/projectname ;
        index  index.html;

        try_files $uri $uri/ /index.html?$args;
    }
}

观察:当我访问配置的域时,它会路由到 nginx 默认页面,而不是显示预期的项目。

4

2 回答 2

1

3 Changes:

  1. Instead of rewrite, do return 301
  2. In second server block, don't have /$1 at the end of server_names
  3. remove index index.html from location /projectname block
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com/$request_uri;
}

server {
    listen       443 ssl;
    listen       [::]:443 ssl;

    ssl_certificate "/etc/ssl/XX.pem";
    ssl_certificate_key "/etc/ssl/XX.key";
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    server_name  example.com www.example.com;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;

    location /projectname {
        root /usr/share/nginx/html/projectname ;

        try_files $uri $uri/ /index.html?$args;
    }
}

Try this and it should work.

于 2021-04-26T17:35:29.593 回答
0

1:在你的 Linux 机器上打开 sudo vi /etc/hosts 文件

2: 127.0.0.1 example.com www.example.com

3:保存退出。

于 2021-04-23T06:21:44.770 回答