Nginx多个服务器块侦听同一端口

24 nginx

我想运行www.example.comapi.example.com同一端口上80.

这就是我所拥有的.我所有的谷歌ping导致下面的代码.但是,这不起作用.

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

server {
        listen 80
        server_name api.example.com

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}
Run Code Online (Sandbox Code Playgroud)

我不知道原因.有什么建议吗?

谢谢!

C1s*_*sc0 20

在分别创建两个文件(你没有,但它会更清晰)/etc/nginx/sites-available/www.example.com/etc/nginx/sites-available/api.example.com

api.example.com文件的内容

server {
        listen 80
        server_name api.example.com
        root /var/www/api.example.com/html/example/app; #also add a root dir here
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}
Run Code Online (Sandbox Code Playgroud)

www.example.com的内容:

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}
Run Code Online (Sandbox Code Playgroud)

最后启用域: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.comsudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

  • -1这只是一个方便.您不必在多个文件中分隔服务器块,并且它不能解决您遇到的任何配置问题,除非由于nginx以错误的顺序加载confs而导致的任何问题(您至少可能导致这些问题) . (24认同)