繁体   English   中英

Nginx Config 2 Web 一台服务器上的应用程序

[英]Nginx Config 2 Web Applications on one Server

早上好家伙,

我正在尝试创建一个有效的 Nginx 配置。

我有两个 web 应用程序:

  • /应用程序/网络
  • /应用程序/api

我的 URL 应该是这样的:

  • 10.XXXX => /app/web

  • 10.XXXX/api => /app/api

我目前的配置:

server {
    listen 80 default_server;
 
    index index.php index.html index.htm;

    root /app/web;

    location /api {
      root /app/api;
    }

    location ~ [^/]\.php(/|$) {
        autoindex on;
        autoindex_exact_size on;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log  stderr warn;
    access_log  /dev/stdout main;

    client_max_body_size 10M;
}

有什么建议么?

  1. 只需将相同的配置复制并粘贴到新文件中即可
  2. 将其命名为与您希望它运行的子域相同
  3. 给出文件夹的路径
  4. 将新的子域添加到 hosts 文件
  5. 重启nginx

在 nginx 中再创建 2 个服务器。 第一个用于 /api(例如听 8080),另一个用于 /web(在 8081 上)。 然后,您的主要服务器(在 80/443 上)是其他服务器的代理:

upstream backend_api{
    server 127.0.0.1:8080;
}

upstream backend_web{
    server 127.0.0.1:8081;
}

server {
    listen    80;
    server_name    www.example.com example.com;

    location /api{
        include proxy_params;
        proxy_pass http://backend_api;
    }

    location / {
        include proxy_params;
        proxy_pass http://backend_web;
    }
}

server {
    listen 8080 default_server;
 
    index index.php index.html index.htm;

    root /app/api;
    
    location ~ [^/]\.php(/|$) {
        autoindex on;
        autoindex_exact_size on;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log  stderr warn;
    access_log  /dev/stdout main;

    client_max_body_size 10M; }

server {
    listen 8081 default_server;
 
    index index.php index.html index.htm;

    root /app/web;
   
    location ~ [^/]\.php(/|$) {
        autoindex on;
        autoindex_exact_size on;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log  stderr warn;
    access_log  /dev/stdout main;

    client_max_body_size 10M;
}

如果您遵循以下配置,您可以托管多个站点。 这是一个工作代码。 您可以根据需要进行修改

server {
    # Listing port and host address
    # If 443, make sure to include ssl configuration for the same.
    listen 80; 
    listen [::]:80;

    server_name 192.168.0.132;

    # Default index pages
    index index.php;

    # Root for / shipment
    root /var/www/msdsl/shipment/public;

    # Handle main root / shipment
    location / {
        #deny all;
        try_files $uri $uri/ /index.php?$args;
    }

    # Handle restora project, just replicate this section for further projects app3, app4 
    # by just replacing restora with appropriate tag(project1,/project2/project 3)
    location /restora {
        # Root for this project
        root /var/www/msdsl/restora/public;

        # Rewrite $uri=/restora/xyz back to just $uri=/xyz
        rewrite ^/restora/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

    location /tposreport {
        # Root for this project
        root /var/www/msdsl/tposreport/public;

        # Rewrite $uri=/tposreport/xyz back to just $uri=/xyz
        rewrite ^/tposreport/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

    # Handle all locations *.php files (which will always be just /index.php)
    # via factcgi PHP-FPM unix socket
    location ~ \.php$ {
        # At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /restora/xyz.
        # But we don't want to pass /restora/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below. 
        # This allows laravel to see /restora/xyz as just /xyz in its router.  
        # So laravel route('/xyz') responds to /restora/xyz as you would expect.
        set $newurl $request_uri;

        if ($newurl ~ ^/tposreport(.*)$) {
                set $newurl $1;
                root /var/www/msdsl/tposreport/public;
        }

        if ($newurl ~ ^/restora(.*)$) {
                set $newurl $1;
                root /var/www/msdsl/restora/public;
        }

        # Pass all PHP files to fastcgi php fpm unix socket
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Use php fpm sock which is installed on your machine like php7.2, php5.6
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; 
        fastcgi_index index.php;
        include fastcgi_params;
        # Here we are telling php fpm to use updated route that we've created to properly
        # response to laravel routes.
        fastcgi_param REQUEST_URI $newurl;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    # Deny .ht* access
    location ~ /\.ht {
        deny all;
    }
}`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM