Nginx 常用设置

Posted on May 10, 2020
Nginx常用设置参考: PHP站点/反向代理/webdav

1. 安装

# debian
apt install nginx nginx-extras 

2. PHP站点

apt install php-fpm systemctl enable –now php7.3-fpm.service

# wordpress
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

    location /wordpress {
        alias /var/www/html/wordpress;
        autoindex on;
        autoindex_exact_size off;
        autoindex_format html;
        autoindex_localtime on;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

3. 反向代理

# gitea
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

    location ^~ /gitea {
        return 301 $scheme://$host/gitea/;
    }

    location /gitea/ {
        client_max_body_size 512M;

        # make nginx use unescaped URI, keep "%2F" as is
        rewrite ^ $request_uri;
        rewrite ^/gitea(/.*) $1 break;
        proxy_pass http://127.0.0.1:8081$uri;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4. Webdav

apt install libnginx-mod-http-dav-ext libnginx-mod-http-auth-pam
sudo usermod -aG shadow www-data
mkdir /webdav
chown -R www-data:www-data /webdav

/etc/nginx/conf.d/server.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

    location /webdav {
        alias /webdav/;
        dav_methods PUT DELETE MKCOL COPY MOVE;
        dav_ext_methods PROPFIND OPTIONS;
        dav_access user:rw group:rw all:rw;

        client_max_body_size 0;
        create_full_put_path on;
        client_body_temp_path /tmp;

        autoindex on;
        autoindex_exact_size off;
        autoindex_format html;
        autoindex_localtime on;

        # Basic authentication setup
        # auth_basic "Restricted Access";
        # auth_basic_user_file /etc/nginx/webdav.passwd;

        # auth_pam "Restricted";
        # auth_pam_service_name "common-auth";
    }
}