如何在 nginx/sites-available/default 上使用 sed 查找和编辑多行并替换它们

如何在 nginx/sites-available/default 上使用 sed 查找和编辑多行并替换它们

我需要取消注释默认文件中的现有文本

此站点中的文件与我的默认文件非常相似

https://salsa.debian.org/nginx-team/nginx/blob/master/debian/conf/sites-available/default

不同之处在于,因为我使用 https 证书,所以我在默认文件中有另一个 server{} 部分,并且在其中我又出现了下一部分...所以我需要在第一个 server{} 部分中找到并将 info.php 添加到带有箭头的行中,我已创建箭头来演示我想在哪一行添加 info.php

服务器 {

    # Add index.php to the list if you are using PHP
    --> index index.html index.htm index.nginx-debian.html;

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            #fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            #include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
            #deny all;
    #}

    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    #server {
    #       listen someport;
    #       listen [::]:someport;
    #
    #       server_name example.com;
    #
    #       root /var/www/example.com;
    #       index index.html;
    #
    #       location / {
    #               try_files $uri $uri/ =404;
    #       }
    #}

}

然后在下一个服务器{}部分搜索,在此部分我需要更改以下内容:

服务器 {

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name mydomainname.com; # managed by Certbot

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            #fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            #include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
            #deny all;
    #}
    listen [::]:someport ssl ipv6only=on; # managed by Certbot
    listen someport ssl; # managed by Certbot

}

更改为:

服务器 {

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name mydomainname.com; # managed by Certbot

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;

    }
    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
    listen [::]:someport ssl ipv6only=on; # managed by Certbot
    listen someport ssl; # managed by Certbot

}

我已经尝试过 sed,但是我对它的语法不太熟悉,因为我遇到了有关其中特殊字符的各种错误,有人可以帮我解决这个问题吗?

答案1

真的,你不能...

sed可以匹配模式并从中编辑数据,但它实际上无法看出注释和命令行之间的区别。

由于它是一个很小的文件,因此在某些编辑器中打开它并手动进行编辑要容易得多 - 使用 vi,nano,gedit 或任何您喜欢的编辑器。

相关内容