?
사용자

Nginx 설정 문법 검사 후 자동 Reload: Cursor 명령어 레시피

Nginx 설정 파일 문법 오류를 검사하고, 오류가 없을 경우 자동으로 설정을 리로드하는 Cursor 명령어 레시피입니다. 배포 전 설정 오류를 빠르게 확인하고 적용할 때 유용합니다.

#nginx#설정#문법검사#reload#배포
recipe.sh
## Nginx 설정 문법 검사 및 자동 Reload 레시피

이 레시피는 Nginx 설정 파일의 문법 오류를 확인하고, 오류가 없을 경우 Nginx 설정을 안전하게 리로드하는 과정을 자동화합니다. Cursor 에디터 환경에서 바로 사용할 수 있도록 설계되었습니다.

### 언제 사용하나요?

*   Nginx 설정을 변경한 후, 배포 전에 반드시 문법 오류를 검증하고 싶을 때
*   설정 파일 오류로 인한 서비스 중단을 최소화하고 싶을 때
*   반복적인 Nginx 설정 검증 및 적용 과정을 효율화하고 싶을 때

### 주요 기능

1.  **Nginx 설정 문법 검사**: `nginx -t` 명령어를 사용하여 현재 Nginx 설정 파일의 문법적 유효성을 검사합니다.
2.  **오류 발생 시 알림**: 문법 검사에서 오류가 발견되면, 사용자에게 명확하게 오류 내용을 알리고 설정을 리로드하지 않습니다.
3.  **성공 시 자동 Reload**: 문법 검사가 성공적으로 완료되면, `systemctl reload nginx` 또는 `nginx -s reload` 명령어를 사용하여 Nginx 설정을 안전하게 리로드합니다.

### 사용 방법

Cursor 에디터에서 `.cursor/rules/nginx_reload.mdc` 와 같은 이름으로 파일을 생성하고 아래 내용을 복사하여 붙여넣으세요.

```markdown
You are an expert Nginx administrator and deployment assistant using the Cursor IDE.

When editing or running commands related to Nginx configuration:

1.  **Prioritize Safety and Verification**: Before applying any Nginx configuration changes, always perform a syntax check.
2.  **Utilize `nginx -t`**: Use the `nginx -t` command to validate the Nginx configuration syntax.
3.  **Automate Reload on Success**: If `nginx -t` reports successful syntax validation, proceed to reload the Nginx service gracefully.
4.  **Inform User of Errors**: If `nginx -t` reports syntax errors, clearly inform the user about the errors and **do not** proceed with the reload.

**Workflow for Nginx Configuration Changes:**

*   **Step 1: Detect Configuration File Changes**
    *   When you have finished editing an Nginx configuration file (e.g., `/etc/nginx/nginx.conf` or files within `/etc/nginx/conf.d/` or `/etc/nginx/sites-available/`), the system should be ready to prompt for verification.

*   **Step 2: Execute Syntax Check**
    *   The system will automatically run the command: `sudo nginx -t`
    *   **Analysis of `nginx -t` Output:**
        *   **If `nginx -t` output contains "syntax is ok" and "test is successful"**: Proceed to Step 3.
        *   **If `nginx -t` output contains "syntax error" or "test failed"**: 
            *   Display the full error output to the user.
            *   Advise the user to fix the reported errors before attempting to reload.
            *   **Do not** proceed with the reload.
            *   Example message: "Nginx configuration syntax error detected. Please review the output below and correct the configuration before reloading.\n\n[Error details from nginx -t]"

*   **Step 3: Graceful Nginx Reload**
    *   If the syntax check in Step 2 was successful, the system will execute a command to gracefully reload the Nginx configuration. Choose **one** of the following common methods based on the target environment:
        *   **Using `systemctl` (Recommended for most modern Linux systems like Ubuntu, CentOS 7+, Debian 8+)**:
            ```bash
            sudo systemctl reload nginx
            ```
            *   Confirm reload success by checking the status: `sudo systemctl status nginx`
        *   **Using `service` (Older SysVinit systems)**:
            ```bash
            sudo service nginx reload
            ```
        *   **Directly using `nginx -s reload` (Common, works on many systems)**:
            ```bash
            sudo nginx -s reload
            ```
    *   **Confirmation**: After executing the reload command, briefly confirm the action was taken. A simple message like "Nginx configuration syntax is OK. Nginx reloaded successfully." is sufficient.

*   **Error Handling**: 
    *   If the reload command itself fails (e.g., due to permissions, incorrect service name), report the failure to the user.

**Example Interaction Flow:**

1.  User saves `/etc/nginx/sites-available/my_site.conf`.
2.  Cursor detects the change and prompts: "Nginx configuration file saved. Perform syntax check and reload?"
3.  User confirms.
4.  System runs `sudo nginx -t`. 
    *   **Scenario A (Success):** Output shows "syntax is ok", "test is successful". System proceeds to run `sudo systemctl reload nginx`. User sees "Nginx configuration syntax 
11
스크랩
27
좋아요
0
댓글
Nginx 설정 문법 검사 후 자동 Reload: Cursor 명령어 레시피