Using apache mod_wsgi test error ---windows environment - apache

I have seen many solutions similar to my problem, but there is no one that suits me. Please help me solve it.
First, I want to test my apache's mod_wsgi.
httd.conf:
LoadFile "e:/zt_6.27/python/python35.dll"
LoadModule wsgi_module "e:/zt_6.27/python/lib/site-
packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
WSGIPythonHome "e:/zt_6.27/python"
<VirtualHost *:80>
WSGIScriptAlias /myapp E:\zt_6.27\Apache24\htdocs\myapp.wsgi
<Directory 'E:\zt_6.27\Apache24\htdocs'>
Require all granted
Require host ip
Allow from all
</Directory>
</VirtualHost>
myapp.wsgi:
def application(environ,start_response):
status='200 OK'
output='Hello wsgi!'
response_headers=[('Content-type','text/plain'),('Content-Length',str(len(output)))]
start_response(status,response_headers)
return[output]
I restart my apache service,call "http://localhost/myapp".
It return 500 Internal Server Error.
I open the error.log of apache.
it show:
[Fri Aug 10 10:33:35.803119 2018] [wsgi:error] [pid 9252:tid 1408] [client
::1:57261] mod_wsgi (pid=9252): Exception occurred processing WSGI script
'E:/zt_6.27/Apache24/htdocs/myapp.wsgi'.
[Fri Aug 10 10:33:35.803119 2018] [wsgi:error] [pid 9252:tid 1408] [client
::1:57261] TypeError: sequence of byte string values expected, value of type
str found\r
I checked the code carefully and it is no problem.
What did I do wrong?
Other than this,I successfully ran this code yesterday, it went wrong today.
help me please!!

If using Python 3, you must use:
def application(environ,start_response):
status = '200 OK'
output = b'Hello wsgi!'
response_headers = [('Content-type','text/plain'),('Content-Length',str(len(output)))]
start_response(status,response_headers)
return[output]
That is, change to:
output = b'Hello wsgi!'
The list returned, must have byte strings in it, not unicode strings.
To return unicode strings you would need to convert them to byte strings. I cheated here by just marking it as byte string to start with, but you should encode them to byte strings as UTF-8 instead.
I would suggest using a micro framework like Flask as it will handle these sorts of details for you.

Related

How to customize Apache Error Log Format?

According to Apache documentation a user should be able to change the error log format. The following example shows what supplementary information is logged in the error log in addition to the actual log message.
ErrorLogFormat "[%t] [%l] [pid %P] %F: %E: [client %a] %M"
I am interested only in the log message "%M". I do not want to see anything else in the log file.
Therefore, in my vhost configuration I added the following line.
<VirtualHost *:80>
...
ErrorLogFormat "%M"
...
</VirtualHost>
This configuration removed "pid", "IP address", etc. But two strings (Apache error code AH01215 and cgi script path) which are not even mentioned in ErrorLogFormat are still added to the log messages. The Apache log looks like this:
AH01215: My log message1: /var/www/localhost/cgi-bin/script.cgi
AH01215: My log message2: /var/www/localhost/cgi-bin/script.cgi
...
I would appreciate if someone could tell me how to suppress this cgi script path string "/var/www/localhost/cgi-bin/script.cgi" as well as Apache error code "AH01215".
You are probably using the Apache mod cgi that is where the error code is added. The code comes in %M, so you can't remove it with the log format.
Switch to cgid to remove the AH01215 from the log. This fixed it for me.
In Ubuntu:
a2dismod cgi
a2enmod cgid

How to remove "allowmethods:error" entry in apache error_log

I have only allowed GET, POST methods in my apache server. It shows lot of times error like below which is of no use to me. How can I block these errors to come in apache error log
[Mon Aug 22 18:43:27.232168 2016] [allowmethods:error] [pid 19314:tid 139797637039872] [demowebsite.com] [client 224.0.0.0:80] AH01623: client method denied by server configuration: 'PURGE' to /var/www/demowebsite/
I also want to know what is causing it. I am using apache 2.4 + php 5.5 + mod_pagespeed + varnish.
Please help me.
Since you seem to be using Apache 2.4.X
Just by setting:
LogLevel allowmethods:crit
you will be rising the level necessary to log to error log to critical level in that module so they won't show up for errors.

Seemingly random header errors with mod_perl - bad requests or something else?

We're running mod_perl on Apache 2 and get seemingly random header related errors that we just can't figure out. Due to the nature of the site we get hit by a ton of bots, so I'm thinking these are caused by bad or malformed requests from bots, but I'd like to figure it out for sure one way or another so I know where to go from here. Here's an example of the 2 most common errors we see in the logs:
[Thu Nov 13 21:40:48 2014] [warn] /whatever did not send an HTTP header
[Thu Nov 13 21:40:48 2014] [error] [client x] malformed header from script. Bad header=\x86z\x03d\x86z\x03d\x86z\x03d\x86z\x03d\x86z\x03d\x86z\x03d\x86z\x03d\x86z: index.cgi
[Fri Nov 14 00:04:17 2014] [warn] /whatever did not send an HTTP header
[Fri Nov 14 00:04:17 2014] [error] [client x] Premature end of script headers: index.cgi
We get 100s of 1,000s of requests to these same URLs daily, and they work fine 99.999% of the time. I don't believe it's our scripts - we always output correct headers. No real users have ever complained about any errors on our site, etc. so I'm hoping this is just caused by some bad requests from bots.
And if so, what if anything can we do to make these stop? It's a real pain because these errors trip our monitoring systems and my techie gets about 20-30 fake error alerts every day.
Turns out it was a problem with Safari browsers and mod_deflate compression.
The simple solution:
BrowserMatch Safari gzip-only-text/html

mod_proxy 502 Proxy Error when upload a file

I'm trying to configure the following environment: a VPS running apache and mod_proxy to proxy another server running at home (the backend). I'm able to download files but when I try to upload files the POST request fails with this error:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request POST /upload/upload.php.
Reason: Error reading from remote server
What I don't understand is why it works for files as low as 500 bytes. And it does quickly! However, when I try to upload a tiny 4kb file, it takes forever until the error is reached. As expected, the upload works flawlessly when the backend is accessed directly, without the VPS. I tried many configurations for both sides, also tried to increase the timeout but I don't think this is the way to go. The backend has mod_access installed and it doesn't log anything when the file upload fails.
The apache logs the following:
[Thu Nov 07 22:26:03.044309 2013] [proxy_http:error] [pid 9173] (70007)The timeout specified has expired: [client 177.148.252.99:54097] AH01102: error reading status line from remote server myhome.com, referer: http://frontend.com/upload/
[Thu Nov 07 22:26:03.044423 2013] [proxy:error] [pid 9173] [client 177.148.252.99:54097] AH00898: Error reading from remote server returned by /upload/upload.php, referer: http://frontend.com/upload/
The VPS is running Apache 2.4.6 and the server running at home is a Lighttpd 1.4.32 with SSL.
The virtual host redirecting to the backend is configured as follows:
<VirtualHost *:80>
ServerAdmin webmaster#frontend.com
ServerName frontend.com
ProxyPass / http://backend.com/
ProxyPassReverse / http://backend.com/
</VirtualHost>
Front-end:
http://frontend.com/upload/
Back-end:
http://backend.com/upload/
Do you have any ideas?
The error you're seeing is due to a timeout of the proxy connection to the back-end system. You need to set the ProxyTimeout value to something larger than the default. I would recommend that you start with a value of 60 seconds and see how that works.
ProxyTimeout 60
In addition, I agree with Varghese that you want to set the environment variable in order to configure the connection to send the data in chunks. Unfortunately, there is some confusion over whether the correct setting should be, so you can try either of these:
SetEnv proxy-sendchunked 1
or
SetEnv proxy-sendchunks 1
Good luck. It's a frustrating problem.
Environment variables availables in mod_proxy:
https://httpd.apache.org/docs/2.4/mod/mod_proxy_http.html
I have faced the similar kind of issue and got resolved with the statement, I put after the ProxyPassReverse statement.
The command you have to use is : SetEnv proxy-sendchunks 1

How do I fix this apache error log issue? Mod Deflate

I'm getting the following errors in my erorr.log file on every request
[Fri Jan 29 14:44:17 2010] [debug] mod_deflate.c(619): [client 10.128.99.99] Zlib: Compressed 6025 to 1847 : URL
about 2 gigs worth (high load server)
any idea what this error is referring to?
Make sure you only have LogLevel specified once, or that you're changing it for the correct virtual host. And you'll need to kick apache of course.
doh! just found it... someone had set a specific error log for this particular virtual host and the loglevel was set to debug.