octobercms / october

Self-hosted CMS platform based on the Laravel PHP Framework.
https://octobercms.com/
Other
11.02k stars 2.21k forks source link

only index page is ok, all the others url is 404 nginx #3550

Closed dexterque closed 6 years ago

dexterque commented 6 years ago
Expected behavior

http://35.234.19.00/demo/ajax want see the demo page

Actual behavior

image

Reproduce steps
  1. I use lnmp env, I didn't install database (php artisan october:install)
  2. blow is my config /usr/local/nginx/conf/vhost/www.october.net.conf server { listen 80;

    listen [::]:80;

    server_name  35.234.19.00  www.october.net october.net;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /home/wwwroot/default/october;
    include none.conf;
    #error_page   404   /404.html;
    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
    include enable-php.conf;
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }
    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }
    location ~ /.well-known {
        allow all;
    }
    location ~ /\.
    {
        deny all;
    }
    access_log  /home/wwwlogs/www.october.net.log;

    }

Really need some help. Thanks!

dexterque commented 6 years ago

ok, I found someone have the same question http://octobercms.com/forum/post/problem-installing-on-nginx-server blew is my correct config: server

{
listen 80;
#listen [::]:80;
server_name 35.234.19.00 www.october.net october.net;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/default/october;
include none.conf;
#error_page 404 /404.html;
#Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/..php$ { deny all; }
include enable-php.conf;

#**_add_**
location / {
    root /home/wwwroot/default/october;
    #First attempt to serve request as file, then
    #as directory, then fall back to index.php
    try_files $uri $uri/ /index.php;
 }

location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /.
{
deny all;
}
access_log /home/wwwlogs/www.october.net.log;
}
LukeTowers commented 6 years ago

@dexterque so is your problem solved then?

viamage commented 6 years ago

@dexterque find our nginx production config below, it works with dozens of our octobers and includes some injection, bad bots and private files blocks that may be useful.

server {
    listen 80;
    server_name DOMAIN;
    return 301 https://DOMAIN$request_uri;
}

server {
    # main properties
    # listen 80;
    server_name DOMAIN;
    root ROOT_PATH;
    index index.php index.html index.htm;
    gzip on;
    gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

    # ssl settings
    #listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/DOMAIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/DOMAIN/privkey.pem;

    # Check if file exists
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # for letsencrypt
    location ~ "^/.well-known/" {
        allow all;
    }

   # block private files
    location ~ /bootstrap/.*$ {
        rewrite ^/bootstrap/.* / break;
    }

    location ~ /config/.*$ {
       deny all;
        rewrite ^/config/.* / break;
    }

    location ~ /vendor/.*$ {
        rewrite ^/vendor/.* / break;
    }

    location /storage$ {
        rewrite ^/storage/cms/.* / break;
        rewrite ^/storage/logs/.* / break;
        rewrite ^/storage/framework/.* / break;
        rewrite ^/storage/temp/protected/.* / break;
        rewrite ^/storage/app/uploads/protected/.* / break;
    }

    # main location for php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # 404 for yaml
    location ~* (\.yaml) {
        return 404;
    }

    # 403 for .git
    location ~ /\.git {
        access_log off;
        log_not_found off;
        return 403;
    }

    # 403 for composer.json
    location ~ /composer.json {
        access_log off;
        log_not_found off;
        return 403;
    }

    # 403 for internal .php
    location ~ \..*/.*\.php$ {
        return 403;
    }

    # max cache for resources
    location ~* ^.+\.(css|js|png|jpg|gif|jpeg|ico|woff|woff2|ttf|otf|eot|svg)$ {
        access_log off;
        expires max;
    }

    # deny for .ht* (let's keep it)
    location ~ /\.ht {
        deny all;
    }

    location ~ /\.env {
       deny all;
    }

    # primary php processing
    location ~ \.(hh|php)$ {
        proxy_intercept_errors on;
        error_page 502 = @fallback;
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
        server_tokens off;
    }

    # fallback php processing
    location @fallback {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
    }

    location /sitemap {
        rewrite ^/sitemap-(.*).xml /storage/sitemaps/sitemap-$1.xml break;
    }

    ## Block SQL injections
    set $block_sql_injections 0;
    if ($query_string ~ "union.*select.*\(") {
        set $block_sql_injections 1;
    }
    if ($query_string ~ "union.*all.*select.*") {
        set $block_sql_injections 1;
    }
    if ($query_string ~ "concat.*\(") {
        set $block_sql_injections 1;
    }
    if ($block_sql_injections = 1) {
        return 403;
    }

    ## Block file injections
    set $block_file_injections 0;
    if ($query_string ~ "[a-zA-Z0-9_]=http://") {
        set $block_file_injections 1;
    }
    if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
        set $block_file_injections 1;
    }
    if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
        set $block_file_injections 1;
    }
    if ($block_file_injections = 1) {
        return 403;
    }

    ## Block common exploits
    set $block_common_exploits 0;
    if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
        set $block_common_exploits 1;
    }
    if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
        set $block_common_exploits 1;
    }
    if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
        set $block_common_exploits 1;
    }
    if ($query_string ~ "proc/self/environ") {
        set $block_common_exploits 1;
    }
    if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
        set $block_common_exploits 1;
    }
    if ($query_string ~ "base64_(en|de)code\(.*\)") {
        set $block_common_exploits 1;
    }
    if ($block_common_exploits = 1) {
        return 403;
    }

    ## Block spam
    set $block_spam 0;
    if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
        set $block_spam 1;
    }
    if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
        set $block_spam 1;
    }
    if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
        set $block_spam 1;
    }
    if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
        set $block_spam 1;
    }
    if ($block_spam = 1) {
        return 403;
    }

    ## Block user agents
    set $block_user_agents 0;

    # Don't disable wget if you need it to run cron jobs!
    #if ($http_user_agent ~ "Wget") {
    #    set $block_user_agents 1;
    #}

    # Disable Akeeba Remote Control 2.5 and earlier
    if ($http_user_agent ~ "Indy Library") {
        set $block_user_agents 1;
    }

    # Common bandwidth hoggers and hacking tools.
    if ($http_user_agent ~ "libwww-perl") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "GetRight") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "GetWeb!") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "Go!Zilla") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "Download Demon") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "Go-Ahead-Got-It") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "TurnitinBot") {
        set $block_user_agents 1;
    }
    if ($http_user_agent ~ "GrabNet") {
        set $block_user_agents 1;
    }

    if ($block_user_agents = 1) {
        return 403;
    }

    # Kindly leave that here. "A man is not dead while his name is still spoken." http://www.gnuterrypratchett.com/
    add_header X-Clacks-Overhead "GNU Terry Pratchett";
}
dexterque commented 6 years ago

@LukeTowers yes, it seems that I can't close the issure, please close it. @viamage Thanks for your userful nginx config, I'm newbie to nginx server, I hardly figure out your comment meaning...