# Apache vhost configuration for IEM

# mod_wsgi is configured from https://github.com/akrherz/iemwebfarm
# DirectoryIndex index.py
# WSGIDaemonProcess iemwsgi_ap processes=24 threads=1 display-name=%{GROUP} maximum-requests=100

Include /opt/iemwebfarm/config/vhost_common.conf

ServerName mesonet.agron.iastate.edu
ServerAlias www.mesonet.agron.iastate.edu
ServerAlias mesonet1.agron.iastate.edu
ServerAlias mesonet2.agron.iastate.edu
ServerAlias mesonet3.agron.iastate.edu
ServerAlias iem.local
ServerAdmin akrherz@iastate.edu
DocumentRoot /opt/iem/htdocs

# http://enable-cors.org/server_apache.html
Header set Access-Control-Allow-Origin "*"

# RewriteEngine is not enabled for vhosts by default
RewriteEngine On
# Useful for debugging
# LogLevel alert rewrite:trace3

Redirect permanent /archive/nexrad https://mesonet-longterm.agron.iastate.edu/archive/nexrad
Redirect permanent /archive/gempak https://mesonet-longterm.agron.iastate.edu/archive/gempak
Redirect permanent /archive/raw https://mesonet-longterm.agron.iastate.edu/archive/raw
Redirect permanent /data/nexrd2/raw https://nomads.ncep.noaa.gov/pub/data/nccf/radar/nexrad_level2

<Directory "/opt/iem/htdocs">
    # Need FollowSymLinks for mod_rewrite to work!
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    # Default handler for python scripts
    WSGIProcessGroup iemwsgi_ap
    # Allow wsgi scripts to emit 404s that can come back to the handler.
    # FIXME this causes all error handling to come back, which is a problem
    # https://github.com/GrahamDumpleton/mod_wsgi/issues/825
    # WSGIErrorOverride On
    AddHandler wsgi-script .py
    Options +ExecCGI
</Directory>

<Directory "/mesonet/ARCHIVE">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory "/mesonet/data">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory "/mesonet/share">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# Point to system-installed mapserv binary (from conda env)
ScriptAlias /cgi-bin/mapserv/mapserv.fcgi ${IEMWEBFARM_MAPSERV}
ScriptAlias /cgi-bin/mapserv/mapserv ${IEMWEBFARM_MAPSERV}
<Location "/cgi-bin/mapserv">
    SetHandler fcgid-script
    Options +ExecCGI
</Location>

Alias /cgi-bin/ "/opt/iem/cgi-bin/"
<Directory "/opt/iem/cgi-bin">
    AllowOverride None
    Options FollowSymLinks
    Require all granted

    # Default handler for python scripts
    WSGIProcessGroup iemwsgi_ap
    AddHandler wsgi-script .py
    AddHandler cgi-script .cgi
    Options +ExecCGI
</Directory>

<LocationMatch "^/one(/index\.html)?$">
    # Prevent caching of index.html
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
</LocationMatch>

Alias /usage "/mesonet/share/usage/mesonet.agron.iastate.edu/"
<Directory "/mesonet/share/usage/mesonet.agron.iastate.edu/">
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
</Directory>

Alias /agclimate/usage "/mesonet/share/usage/agclimate/"
<Directory "/mesonet/share/usage/agclimate/">
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
</Directory>

<Directory "/opt/iem/htdocs/river">
    RewriteRule wfo.phtml index.php
</Directory>

Alias /data "/mesonet/ldmdata"
<Directory "/mesonet/ldmdata">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Require all granted
    Redirect permanent /data/gis/images/unproj /data/gis/images/4326
    Redirect permanent /data/gis/shape/unproj /data/gis/shape/4326
</Directory>

Alias "/onsite/lapses" "/mesonet/share/lapses"
Alias "/onsite/windrose" "/mesonet/share/windrose"
Alias "/onsite/iemre" "/mesonet/data/iemre"
Alias "/onsite/iemre_china" "/mesonet/data/iemre_china"
Alias "/onsite/iemre_europe" "/mesonet/data/iemre_europe"
Alias "/onsite/iemre_sa" "/mesonet/data/iemre_sa"
Alias "/onsite/mrms" "/mesonet/data/mrms"
Alias "/onsite/prism" "/mesonet/data/prism"
Alias "/onsite/stage4" "/mesonet/data/stage4"
Alias "/onsite/era5land" "/mesonet/data/era5"
Alias "/present" "/mesonet/share/present"
Alias "/cases" "/mesonet/share/cases"
Alias "/GIS/data/gis" "/mesonet/data/gis"
Alias "/archive/awos" "/mesonet/ARCHIVE/awos"
Alias "/archive/raw" "/mesonet/ARCHIVE/raw"
# 7 Mar 2026 removeme at some point
Alias "/archive/rer" "/mesonet/ARCHIVE/rer"
Alias "/m/img"     "/mesonet/share/iemmaps"
Alias "/sites/pics"     "/mesonet/share/pics"
Alias "/climodat/reports" "/mesonet/share/climodat/reports"
Alias "/climodat/ks" "/mesonet/share/climodat/ks"

Alias "/pickup" "/mesonet/share/pickup"
<Directory "/mesonet/share/pickup">
    AllowOverride None
    Options FollowSymLinks Indexes MultiViews
    Require all granted
</Directory>

Alias /tmp /var/webtmp
<Directory "/var/webtmp">
    AllowOverride None
</Directory>

<Directory "/opt/iem/cgi-bin/wms/nexrad">
    RewriteRule n0r.cgi /cgi-bin/mapserv/mapserv.fcgi?map=/opt/iem/data/wms/nexrad/n0r.map& [QSA,L]
    RewriteRule n0q.cgi /cgi-bin/mapserv/mapserv.fcgi?map=/opt/iem/data/wms/nexrad/n0q.map& [QSA,L]
</Directory>

<Directory "/opt/iem/cgi-bin/wms/us">
    RewriteRule wwa.cgi /cgi-bin/mapserv/mapserv.fcgi?map=/opt/iem/data/wms/us/wwa.map& [QSA,L]
</Directory>

<Proxy "http://127.0.0.1:9081/*">
    # The backend doesn't really care about the hostname attm
    ProxyPreserveHost Off
    RequestHeader set "Host" "iem.local"
    # Attempt to reduce random proxy noise
    ProxySet connectiontimeout=5 timeout=55 retry=0 disablereuse=On
</Proxy>

ProxyPass /c http://127.0.0.1:9081/c
ProxyPass /cache http://127.0.0.1:9081/cache

<Directory "/opt/iem/htdocs/GIS">
    RewriteRule vtec_(.*).png radmap.php?layers[]=places&layers[]=nexrad&layers[]=cities&layers[]=interstates&layers[]=uscounties&vtec=$1&width=797&height=400
</Directory>

<Directory "/opt/iem/htdocs/agclimate">
    # Lots of old links out there to bad URIs
    RewriteRule index\.(html|pl|php)$ /agclimate/ [R]

    Redirect /agclimate/daily-pics https://mesonet.agron.iastate.edu/agclimate/daily_pics
    Redirect /agclimate/daily_pics https://mesonet.agron.iastate.edu/data/agclimate
    Redirect /agclimate/info.txt https://mesonet.agron.iastate.edu/agclimate/info.phtml

    #
    RewriteRule smplot/([0-9]{1,12})/(.*).png$ isusm.py?t=$1&v=$2
    RewriteRule isusm.csv nmp_csv.py
</Directory>

<Directory "/opt/iem/htdocs/info">
    RewriteRule datasets/([a-z_]+).html$ datasets.php?id=$1 [QSA]
    RewriteRule datasets/?$ datasets.php [QSA]
</Directory>

<LocationMatch "/api/1">
    # As a reminder, we are going via this route to take advantage of caching
    # port 8080 is nginx, which proxies to fastapi on 8000
    # Always immediately retry a connection
    # We wait up to 5 seconds to connect to the remote server
    # We wait up to 60 seconds for the remote server to respond
    ProxyPass "http://iem-web-services.agron.iastate.edu:8080" retry=0 connectiontimeout=5 timeout=60
    ProxyPassReverse "http://iem-web-services.agron.iastate.edu:8080"
    ProxyErrorOverride On 502 503 504
    ErrorDocument 502 /api/proxy_error_handler.py
    ErrorDocument 503 /api/proxy_error_handler.py
    ErrorDocument 504 /api/proxy_error_handler.py
</LocationMatch>

<Location "/archive/data">
    RequestHeader set "Host" "iem-archive.local"
    ProxyPreserveHost Off
    # Send 404s back here
    ProxyErrorOverride On
    # hacky to keep the paths matching in auto-index
    ProxyPass "http://iem-archive.local/archive/data"
    ProxyPassReverse "http://iem-archive.local/archive/data"
</Location>

<Directory "/opt/iem/htdocs/content">
    RewriteRule pil_([A-Z0-9]{3,6}).png$ pil.php?pil=$1
</Directory>

<Directory "/opt/iem/htdocs/sites">
    # Allow for python scripts to act has directories NETWORK/SID/TOOL
    RewriteRule ^([^/]{3,20})/([^/]{3,20})/([^/]+)$ $3.py
</Directory>


<Directory "/opt/iem/htdocs/wx/afos">
    RewriteRule ([0-9]{12})_([A-Z0-9\s]+).png$ text2png.py?e=$1&pil=$2 [B,L]
</Directory>

<Directory "/opt/iem/pylib/iemweb/">
    Require all granted
    WSGIProcessGroup iemwsgi_ap
    AddHandler wsgi-script .py
</Directory>
# note the preceding directory config
WSGIScriptAlias /geojson /opt/iem/pylib/iemweb/dispatch.py
WSGIScriptAlias /json /opt/iem/pylib/iemweb/dispatch.py
WSGIScriptAlias /search /opt/iem/pylib/iemweb/dispatch.py
WSGIScriptAlias /agclimate/ames_precip.py /opt/iem/pylib/iemweb/dispatch.py
WSGIScriptAlias /agclimate/isusm.py /opt/iem/pylib/iemweb/dispatch.py
WSGIScriptAlias /agclimate/nmp_csv.py /opt/iem/pylib/iemweb/dispatch.py

<Directory "/opt/iem/htdocs/iemre">
    RewriteRule daily/([0-9\-]+)/([0-9\.\-]+)/([0-9\.\-]+)/(json) daily.py?date=$1&lat=$2&lon=$3&format=$4
    RewriteRule hourly/([0-9\-]+)/([0-9\.\-]+)/([0-9\.\-]+)/(json) hourly.py?date=$1&lat=$2&lon=$3&format=$4
    RewriteRule multiday/([0-9\-]+)/([0-9\-]+)/([0-9\.]+)/([0-9\.\-]+)/(json) multiday.py?sdate=$1&edate=$2&lat=$3&lon=$4&format=$5
    RewriteRule cum/([0-9\-]+)/([0-9\-]+)/(shp) cum.py?date0=$1&date1=$2&format=$3&base=50&ceil=86
    RewriteRule cumcounty/([0-9]+)/([0-9\-]+)/([0-9\-]+)/([0-9]+)/([0-9]+)/(json) cum.py?county=$1&date0=$2&date1=$3&format=$6&base=$4&ceil=$5
</Directory>

<Directory "/opt/iem/htdocs/kml">
    RewriteRule roadcond.kml roadcond.php
    RewriteRule roadcond_v2.kml roadcond.php?linewidth=6&maxtype=2
</Directory>

<Directory "/opt/iem/htdocs/nws">
    RewriteRule days_since_([A-Z][A-Z])_([A-Z]).png /plotting/auto/plot/92/phenomena:$1::significance:$2::dpi:100.png
</Directory>

<Directory "/opt/iem/htdocs/onsite/features">
    RewriteRule tags/([^\.]+)\.html tags/index.php?tag=$1 [L]
    RewriteRule vote/(good|bad|abstain).json$ vote.py?vote=$1
    RewriteRule vote.json$ vote.py
    RewriteRule ^[0-9]{4}/[0-9]{2}/.*$ content.py [PT]
</Directory>

<Directory "/opt/iem/htdocs/current">
    RewriteRule live/(.*).jpg live.py?id=$1 [L]
</Directory>

<Directory "/opt/iem/htdocs/plotting/auto">
    RewriteRule qrcode/([0-9]{1,12})/(.*).png$ gen_qrcode.py?p=$1&q=$2&fmt=png [B]
    RewriteRule plot/([0-9]{1,12})/(.*).(png|csv|txt|xlsx|js|geojson|pdf|svg|geotiff)$ autoplot.py?p=$1&q=$2&fmt=$3 [B]
    RewriteRule meta/([0-9]{1,12}).json meta.py?p=$1 [QSA,B]
</Directory>

<Directory "/opt/iem/htdocs/request">
    RewriteRule maxcsv/(.*).txt$ maxcsv.py?q=$1

    RewriteRule grx/iadot_trucks.txt grx/iadot_trucks.py
    # Legacy link was removed 5 Jan 2015
    RewriteRule grx/idot_trucks.php grx/iadot_trucks.py
    # PHP replaced by python 24 May 2020
    RewriteRule grx/time_mot_loc.(php|txt) grx/time_mot_loc.py [QSA]

    # .php was legacy stuff, lets not break old paths
    RewriteRule grx/l3attr.(php|txt) grx/l3attr.py [QSA]
</Directory>

<Directory "/opt/iem/htdocs/roads">
    RewriteRule kcau.jpg kcau.php
    RewriteRule ktiv.jpg ktiv.php
    RewriteRule kwwl.jpg kwwl.php?v2
</Directory>

<Directory "/opt/iem/htdocs/vtec">
    RewriteRule ^event/.* index.py [L]
    RewriteRule ([^\.]+)\.html index.py?vtec=$1 [L]
    RewriteRule f/(.*)$ f.py [QSA]
</Directory>

<Directory "/opt/iem/htdocs/metadata/xml">
    RewriteRule pl_(.*)_(.*).xml pl.py?network=$1&station=$2 [QSA]
    RewriteRule sd_(.*)_(.*).xml sd.py?network=$1&station=$2 [QSA]
</Directory>
